我使用的是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;
请在网址中将双引号替换为单引号,
$url = 'http://www.weather-forecast.com/locations/'.$city.'/forecasts/latest';
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句