2013-07-31 34 views
0

所以我做了這個腳本,基本上是一個命令。未定義指數天氣

它所做的是,用戶使用$ arg定義命令「weather」,其中$ arg就是所說的內容。然後對生成的json進行解碼,然後僅從json發送包。但它說「位置」和「溫度」是不確定的。

有人可以幫我解決這個問題嗎?數據包得到正確發送,但數據包發送時不顯示位置或溫度。數據包sm =發送消息。

這裏是代碼

if($cmd == 'weather'){ 
$file = file_get_contents('http://weather3.pricop.info/api.php?city=' . $arg); 
$weather = json_decode($file, true); 
$user->sendPacket("%xt%sm%-1%0%" . " Temperature for " . $weather["location"] . "%" . " will be " . $weather["temperature"] . "%"); 
} 
+0

請出示'的var_dump($文件);' - 我們真的不能調試爲什麼你的JSON也不是沒有看到JSON ;-) – DaveRandom

+0

HTTP解碼權://prntscr.com/1io71v – user2524169

回答

2

的迴應是這樣的:

{"apiVersion":"1.0", "data":{ "location":"Pascagoula, MS", "temperature":"79", "skytext":"Mostly Cloudy", "humidity":"84", "wind":"6", "date":"2013-07-31", "day":"Wednesday" } } 

返回的數據有數據屬性裏面,所以你必須引用位置屬性。它作爲

$weather["data"]["location"]; 

$weather["data"]["temperature"]; 
+0

非常感謝!有用 :) – user2524169