使用我的浏览器访问给定链接时,它的工作正常。但是,当我试图通过cURL访问它时,它只是无法正常工作。
这是我的代码:
<?php
$url='http://example.com';
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata = curl_exec($ch);
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close ($ch);
?>
答案 0 :(得分:2)
你是否100%肯定你在php.ini中启用了lib_curl?空白的cURL响应很可能是由于模块未启用。试试以下内容:
$ch = curl_init( "http://stackoverflow.com" );
var_dump ( curl_exec( $ch ));
如果有输出,页面可能会重定向您,在这种情况下尝试添加
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) // follow redirects
CURLOPT_AUTOREFERER => true // may also help depending on the site...
答案 1 :(得分:1)
你启用了lib curl吗?
$url='http://example.com';
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch,CURLOPT_MAXREDIRS,30);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20100101 Firefox/6.0');
$rawdata = curl_exec($ch);
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
}
echo $rawdata;
curl_close ($ch);