Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP变量作为URL中的参数_Php_Api_Url_Variables_Parameters - Fatal编程技术网

PHP变量作为URL中的参数

PHP变量作为URL中的参数,php,api,url,variables,parameters,Php,Api,Url,Variables,Parameters,我想创建一个天气信息器,通过访问者的IP显示天气预报 我正试图将变量$ip放入URL,但它不起作用。当我放置真正的IP而不是$IP。它可以工作 我做错了什么 $ip=$_SERVER['REMOTE_ADDR']; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=.$ip.&loc


我想创建一个天气信息器,通过访问者的IP显示天气预报
我正试图将变量$ip放入URL,但它不起作用。当我放置真正的IP而不是$IP。它可以工作
我做错了什么

$ip=$_SERVER['REMOTE_ADDR'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=.$ip.&localObsTime&num_of_days=5&format=json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
 if ($outputJson === FALSE) {
 echo 'Error: '.curl_error($ch);
 }

 echo '<pre> ';
 print_r($outputJson);   
 echo '</pre> ';  
$ip=$\u服务器['REMOTE\u ADDR'];
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,”http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=.$ip&localObsTime&num_of_days=5&format=json“;
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_头,0);
$outputJson=curl\u exec($ch);
如果($outputJson==FALSE){
echo“Error:”.curl_Error($ch);
}
回声';
打印(outputJson);
回声';
试试看

"http://...$ip..."
"http://...{$ip}..."
"http://..." . $ip . "...";
试着做

"http://...$ip..."
"http://...{$ip}..."
"http://..." . $ip . "...";

您在
$ip
前后都有一些不必要的点:

使用以下任何一项:

"http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=$ip&localObsTime&num_of_days=5&format=json"

您在
$ip
前后都有一些不必要的点:

使用以下任何一项:

"http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=$ip&localObsTime&num_of_days=5&format=json"

您正在字符串中使用字符串连接运算符。任用

'http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q='.$ip.'&localObsTime&num_of_days=5&format=json'


您正在字符串中使用字符串连接运算符。任用

'http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q='.$ip.'&localObsTime&num_of_days=5&format=json'


您不需要连接字符串,因为您使用的是双引号。所以你要么:


在url中。

您不需要连接字符串,因为您使用的是双引号。所以你要么:


在url中。

当您打印时,是否至少有正确的输出?@Tarek是,它返回json数据(如果IP是实的,不是变量)。当您打印时,是否至少有正确的输出?@Tarek是,它返回json数据(如果IP是实的,不是变量)因为双引号无效。因为双引号无效。