How to solve the problem of getting 301 or 302 redirection URLs from php curl

Source: Internet
Author: User

Use php curl to obtain remote files. The Code is as follows: Copy codeThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.