Home  >  Article  >  Backend Development  >  Solution to the problem of php curl obtaining the URL redirected by 301 or 302_PHP Tutorial

Solution to the problem of php curl obtaining the URL redirected by 301 or 302_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:29:241072browse

When using php's curl to obtain remote files, the code is as follows:

Copy code 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 crawl the page after the jump
ob_start();
curl_exec($ch);
$contents = ob_get_contents();
ob_end_clean();
curl_close($ch);

return $ contents;
}

$contents = getContents($ghurl);
echo $contents;
?>


Generally speaking Under win2003+iis, if you configure php_curl.dll, there will be no problem.

But the author is using linux+apahe2.0+php5.2.12+directadmin (generally foreign hosting companies use this configuration). If the obtained URL has a 301/302 jump, an error will be reported:

curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in ***

Regarding this issue, Google and Baidu are all in long articles English, it's really a headache for those who are not proficient in Linux.
The solution is actually very simple: log in to your directadmin
and find ->>"PHP SafeMode Configuration" -->>Look at the picture below

Set the default Default Safe and Default Open BaseDir to OFF, the problem is Solved.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323411.htmlTechArticleUsing php's curl to obtain remote files, the code is as follows: Copy the code as follows: ? $ghurl = isset($ _GET['id']) ? $_GET['id']:'http://www.baidu.com/'; // php gets function getCon...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn