问题描述
我使用cURL而不是file_get_contents,这在网址上正常工作,直到我使用GET请求变量代替以下网址上的城市。
I am using cURL instead of file_get_contents, which is working fine on the URL until I used a GET request variable in place of the city on the URL below.
在下面使用cURL:(Works)
Using cURL on the following: (Works)
$ url ='http://www.weather-forecast.com/locations/London/预测/最新';
这很好,但是用$ code>:
This works fine, however when substituting 'London' with a variable $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';
我收到一个错误:您正在寻找的页面不存在(404)
我在cURL函数中做错了什么?这似乎与 file_get_contents
完美契合,是否有我遗漏的东西?
What am I doing wrong in my cURL function? This seems to work perfectly with file_get_contents
, is there something I am missing?
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, / p>
Please replace double quotes with single quotes in url,
$url = 'http://www.weather-forecast.com/locations/'.$city.'/forecasts/latest';
这篇关于PHP在URL中使用cURL和GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!