2011-06-25 6 views
1


私は訪問者のIPによる天気予報を示す気象情報提供者を作成したいと思います。
変数$ ipをURLに配置しようとしていますが、動作しません。私はの代わりに実際のIPを置く。$ ip。が動作します。
何が間違っていますか?PHP変数URLのパラメータとして

$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> '; 
+0

print_rを実行すると、少なくとも正しい出力が得られますか? – Tarek

+0

@Tarekはい、それは何も効果がない二重引用符のため、 – Vera

答えて

2

をやってみてください。次のいずれかを使用し

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

あなたが$ip前後にいくつかの不要なドットを持っている

curl_setopt($ch, CURLOPT_URL, "http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=".$ip."&localObsTime&num_of_days=5&format=json"); 
+0

のjsonデータ(IPが実数である場合)を返します。 –

0

あなたは、文字列連結演算子を使用しています文字列の中に。あなたは二重引用符を使用しているので、あなたは文字列を連結する必要はありません

"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' 
1

を使用しますか。次のいずれかを実行します。

curl_setopt($ch, CURLOPT_URL, "http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=$ip&localObsTime&num_of_days=5&format=json"); 

url。

関連する問題