Question about how to get a 301 or 302 redirection URL from phpcurl _ PHP

Source: Internet
Author: User
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. 301 redirection

Use php curl to obtain remote files. 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 yourDirectadmin

Choose> "PHP SafeMode Configuration" --> view

Set the defaultDefault SafeAndDefault Open BaseDirBothOFFThe problem is solved.

Related reading:

PHP-based cURL quick start

PHP curl Sina Weibo mail sending interface

PHP uses CURL to synchronously capture multiple webpages

PHP curl implements get and post

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.