PHP:有时CURL不返回任何内容

当我尝试使用CURL获取内容但很多时候没有返回任何值.. </ p>

注意:</ strong>我在相同的网址上进行测试</ p>

CURL CODE </ p>

  function curltest(  $ url){

$ ch = curl_init();

ncurl_setopt($ ch,CURLOPT_URL,$ url);
ncurl_setopt($ ch,CURLOPT_HEADER,0);

$ agents = array(\ n'Mozilla / 5.0(Macintosh; Intel Mac OS X 10.7; rv:7.0.1)Gecko / 20100101 Firefox / 7.0.1',
'Mozilla / 5.0(X11; U; Linux i686; en-US; rv: 1.9.1.9)Gecko / 20100508 SeaMonkey / 2.0.4',
'Mozilla / 5.0(Windows; U; MSIE 7.0; Windows NT 6.0; en-US)',
'Mozilla / 5.0(Macintosh; U; Intel) Mac OS X 10_6_7; da-dk)AppleWebKit / 533.21.1(KHTML,与Gecko一样)Version / 5.0.5 Safari / 533.21.1'
);
curl_setopt($ ch,CURLOPT_USERAGENT,$ agents [array_rand($ agents) )]);

$ header [0] =“接受:text / xml,application / xml,application / xhtml + xml,”;
$ header [0]。=“text / html; q = 0.9 ,text / plain; q = 0.8,image / png,* / ; q = 0.5“;
$ header [] =”Cache-Control:max-age = 0“;
$ h eader [] =“Connection:keep-alive”;
$ header [] =“Keep-Alive:10”;
$ header [] =“Accept-Charset:ISO-8859-1,utf-8; q = 0.7,
; q = 0.7“;
$ header [] =”Accept-Language:en-us,en; q = 0.5“;
$ header [] =”Pragma:“;

ncurl_setopt( $ ch,CURLOPT_HTTPHEADER,$ header);
curl_setopt($ ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1); //添加这个
curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,20);
curl_setopt($ ch,CURLOPT_TIMEOUT,10);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ ch,CURLOPT_ENCODING,''); \ n $ output = curl_exec($ ch);

if(curl_errno($ ch)){
curl_close($ ch);
$ output = file_get_contents($ url);
}
return $ output;

}
</ code> </ pre>

请提出任何建议或告诉我上述代码中是否需要进行任何更改</ p>
</ div>

展开原文

原文

When I try to get content using CURL but many time does not return any value..

Note : I am tested on same URL

CURL CODE

function curltest($url){

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);

$agents = array(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100508 SeaMonkey/2.0.4',
'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)',
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; da-dk) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1'
);
curl_setopt($ch,CURLOPT_USERAGENT,$agents[array_rand($agents)]);

$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 10";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // ADD THIS
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_ENCODING, '');
$output = curl_exec($ch);

if(curl_errno($ch)){
curl_close($ch);
$output = file_get_contents($url);
}
return $output;

}

Please give any suggestion OR tell me if any changes required in above code

duanjia1865
duanjia1865 为什么要投票?...
6 年多之前 回复

1个回答



点击此处获取curl中的所有选项 php curl options 。 不同的站点使用不同类型的客户端验证。 如果您的卷曲无法在该网站上有效,则可能不会返回任何内容。</ p>
</ div>

展开原文

原文

see here to get all options in curl php curl options. Different sites using different type of client validation. If your curl fails to valid there site, it may return nothing.

dongshenjie3055
dongshenjie3055 您可以使用这些选项使您的卷曲完美CURLOPT_REFERER,CURLOPT_COOKIEJAR,CURLOPT_COOKIEFILE,CURLOPT_REFERER,CURLOPT_SSL_VERIFYPEER。
6 年多之前 回复
dtf76989
dtf76989 我已经看到那个页面..........我只想知道任何其他选项或上述代码中所需的任何更改
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐