gpt4 book ai didi

PHP 在 URL 中使用 cURL 和 GET 请求

转载 作者:可可西里 更新时间:2023-11-01 13:31:58 26 4
gpt4 key购买 nike

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

在以下方面使用 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';

我收到一个错误:您要查找的页面不存在 (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';

关于PHP 在 URL 中使用 cURL 和 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29454540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com