【发布时间】:2015-04-05 05:49:10
【问题描述】:
我使用 cURL 而不是 file_get_contents,它在 URL 上运行良好,直到我使用 GET 请求变量代替下面 URL 上的城市。
在以下内容上使用 cURL:(有效)
$url = 'http://www.weather-forecast.com/locations/London/forecasts/latest';
这很好用,但是当用变量 $city 替换“伦敦”时:
URL: example.com/weather.php?city=London
$city = $_GET['city'];
$city = ucwords($city);
$city = str_replace(" ", "", $city);
$url = 'http://www.weather-forecast.com/locations/".$city."/forecasts/latest';
我收到一个错误:The page you are looking for doesn't exist (404)
我在 cURL 函数中做错了什么?这似乎与file_get_contents 完美配合,我有什么遗漏吗?
cURL 函数
function curl_get_contents($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$contents = curl_get_contents($url);
echo $contents;
【问题讨论】:
-
您在
$url上的报价不匹配。您用单引号打开,然后尝试用双引号关闭中途。 -
在 $url 中尝试城市周围的单引号
-
完美,有人能把它作为答案吗?