简体   繁体   中英

cURL error: Could not resolve host

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 .= "\n". 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

删除urlencode调用。

删除urlencode($url)应该是:

 $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);

代替

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM