cURL错误:无法解析主机

cURL错误:无法解析主机

问题描述:

A similar question has been posted at but i could not find the solution there Curl error Could not resolve host: saved_report.xml; No data record of requested type"

<?php

$url="http://en.wikipedia.org/wiki/Pakistan";
 $ch = curl_init(urlencode($url));
  echo $ch;
  // used to spoof that coming from a real browser so we don't get blocked by some sites
  $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";

  curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
  curl_setopt($ch, CURLOPT_TIMEOUT, 8);
  curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 10);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  $content = curl_exec($ch);



  $info = curl_getinfo($ch);

if ($content === false || $info['http_code'] != 200) {
  $content = "No cURL data returned for $url [". $info['http_code']. "]";
  if (curl_error($ch))
    $content .= "
". curl_error($ch);
  }
else {
  // 'OK' status; format $output data if necessary here:
  echo "...";
}
  echo $content;
  curl_close($ch);
?>

when i paste the same address in browser i am able to access the webpage. but when i run this script i get the error message. Can anyone please help me. Thanks

发布了类似的问题,但我找不到解决方案 Curl错误无法解析主机:saved_report.xml; 没有请求类型的数据记录“ p>

 &lt;?php 
 
 $ url =”http://en.wikipedia.org/wiki/Pakistan  “; 
 $ ch = curl_init(urlencode($ url)); 
 echo $ ch; 
 //用于欺骗来自真实浏览器的内容,因此我们不会被某些网站阻止
 $ useragent =  “Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1)Gecko / 20061204 Firefox / 2.0.0.1”; 
 
 curl_setopt($ ch,CURLOPT_USERAGENT,$ useragent); 
  curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,4); 
 curl_setopt($ ch,CURLOPT_TIMEOUT,8); 
 curl_setopt($ ch,CURLOPT_LOW_SPEED_TIME,10); 
 curl_setopt($ ch,CURLOPT_HEADER,0); 
 curl_setopt(  $ ch,CURLOPT_RETURNTRANSFER,true); 
 
 $ content = curl_exec($ ch); 
 
 
 
 $ info = curl_getinfo($ ch); 
 
 nif($ content === false |  | $ info ['http_code']!= 200){
 $ content =“没有为$ url [”。$ info ['http_code']返回cURL数据。“]”; 
 if(curl_error($ ch)  )
 $ content。=“
”.curl_error($ ch); 
} 
else {
 //'OK'状态;如果需要,请格式化$ output数据:
 echo“...”;  \ N} 
  echo $ content; 
 curl_close($ ch); 
?&gt; 
  code>  pre> 
 
 

当我在浏览器中粘贴相同的地址时,我可以访问该网页。 但是当我运行此脚本时,我收到错误消息。 任何人都可以帮助我。 谢谢 p> div>

Remove the urlencode call.

remove the urlencode($url) it should be:

 $ch = curl_init($url);

Well.

If you remove urlencode() with instantiating your $ch-var, you go just fine. urlencode() is definitely wrong here.

Good:

 $ch = curl_init($url);

Bad:

 $ch = curl_init(urlencode($url));

$ch = curl_init($url);

instead of

$ch = curl_init(urlencode($url));