卷曲请求失败

时间:2011-07-26 20:01:27

标签: php curl

我有这段代码:

$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, "http://www.infoempleo.com/");  
curl_setopt($ch, CURLOPT_POST, false);  
//curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie );  
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_HTTPGET ,true);  
curl_setopt($ch, CURLOPT_HEADER, false);  
curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue'));
  //  curl_setopt($ch,CURLOPT_TIMEOUT,10); 
curl_setopt($ch, CURLOPT_REFERER, '');  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
$homepage = curl_exec($ch);
var_dump($homepage);

但是不起作用,它与google.com合作,但不与http://www.infoempleo.com/合作。 这段代码有什么问题?

我尝试过像http://network-tools.com/这样的在线ping实用程序,服务器没有响应ping请求,但是他们正在响应http标头。 那么我需要在cURL请求中设置什么才能使其工作?

但是这段代码可行

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url );

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");

curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url . "blog/wordpress/wp-login.php");

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);

怎么样?

1 个答案:

答案 0 :(得分:3)

它可能会限制用户代理。尝试改变

//curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");

curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");