繁体   English   中英

PHP在URL中使用cURL和GET请求

[英]PHP Using cURL and GET request in URL

我正在使用cURL而不是file_get_contents,这在URL上正常工作,直到我在下面的URL上使用GET请求变量代替城市。

在以下内容上使用cURL :(作品)

$url = 'http://www.weather-forecast.com/locations/London/forecasts/latest';

这很好,但是当用'变量$city代替'London'时:

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 = 'http://www.weather-forecast.com/locations/'.$city.'/forecasts/latest';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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