我正在使用cURL而不是file_get_contents,这在URL上正常工作,直到我在下面的URL上使用GET请求变量代替城市。
在以下内容上使用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;
答案 0 :(得分:0)
请在网址中用单引号替换双引号,
$url = 'http://www.weather-forecast.com/locations/'.$city.'/forecasts/latest';