How to solve the problem of getting a 301 or 302 redirection URL from phpcurl. When using php curl to obtain remote files, the code is as follows: Copy the code as follows :? $ Ghurlisset ($ _ GET [id])? $ _ GET [id]: www.baidu.com; php obtains functiongetCon and obtains remote files using php curl. the code is as follows:
The code is as follows:
$ Ghurl = isset ($ _ GET ['id'])? $ _ GET ['id']: 'http: // www.baidu.com /';
// Php get
Function getContents ($ url ){
$ Header = array ("Referer: http://www.baidu.com /");
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 30 );
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header );
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // whether to capture the redirected page
Ob_start ();
Curl_exec ($ ch );
$ Contents = ob_get_contents ();
Ob_end_clean ();
Curl_close ($ ch );
Return $ contents;
}
$ Contents = getContents ($ ghurl );
Echo $ contents;
?>
Generally, in win2003 + iis, if you configure php_curl.dll, it will be okay.
However, in linux + apahe2.0 + php5.2.12 + directadmin (this configuration is generally used by foreign host providers), if the obtained url has a 301/302 jump, an error is returned:
Curl_setopt () [function. curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in ***
For this question, google and Baidu are all in a long article of english, which is really a headache if you are not proficient in linux.
The solution is actually very simple: log on to your directadmin
Choose> "PHP SafeMode Configuration" --> view
Set the defaultDefault SafeAndDefault Open BaseDirBothOFFThe problem is solved.
The pipeline code is as follows :? $ Ghurl = isset ($ _ GET ['id'])? $ _ GET ['id']: 'http: // www.baidu.com/'; // php retrieves function getCon...