[英]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.