2012-07-23 72 views
1

我正在使用PHP向Twitter的API提交JSON查詢(使用POST方法),並且包括地理位置座標作爲獲取地理位置數據的標準之一。從返回的JSON結果中,我將特定的值作爲變量存儲到MySQL中(首先轉換爲字符串)。我已成功獲取JSON結果,並可在瀏覽器中查看緯度/經度數據,但無法成功獲取/存儲它們。下面的代碼適用於抓取所有其他字段(文本,created_at等)。任何幫助,將不勝感激。存儲返回Twitter JSON地理位置作爲變量

這是代碼的一部分,我正在解碼JSON結果/爲每個字段分配變量/將變量發送給MySQL。

$data = mysql_real_escape_string($tweet->{'status'}->{'geo'}); 
+0

正如在[mysql_ *'函數的PHP手冊章節介紹](http://www.php .net/manual/en/intro.mysql.php):*不推薦使用此擴展名編寫新代碼。相反,無論是[mysqli](http://www.php.net/manual/en/book.mysqli.php)還是[PDO_MySQL](http://www.php.net/manual/en/ref.pdo應該使用-mysql.php)擴展名。當你選擇一個MySQL API時,請參閱[MySQL API概述](http://www.php.net/manual/en/mysqlinfo.api.choosing.php)以獲得進一步的幫助。* – eggyal 2012-07-23 20:10:25

+0

當你'var_dump ($ tweet - > {'status'} - > {'geo'})'和'var_dump($ data)' – Vatev 2012-07-23 20:25:50

+0

Vatev,'Undefined Class'和'NULL' - 所以我假設我沒有指向到正確的座標。我將如何設置上面的行? IE我想從以下結果中提取「座標」值: https://stream.twitter.com/1/statuses/filter.json?locations=-180,-90,180,90 – user1546818 2012-07-23 20:34:52

回答

0

Twitter API doco注意到,geo場被棄用,因此,你要看看coordinates領域從現在開始。

此字段是可選的,因此它有可能爲空。但是,由於您在位置上進行過濾,因此應該不太可能。如果它存在,你應該得到這樣的事情:

var_dump($tweet->coordinates); 

// Output: 
object(stdClass)[5] 
    public 'type' => string 'Point' (length=5) 
    public 'coordinates' => 
    array 
     0 => float -99.98741865 
     1 => float 21.9250252 

如果不是你只是得到null。要獲得推文的緯度/經度,你應該可以這樣做:

if ($tweet->coordinates != null) { 
    $long = $tweet->coordinates->coordinates[0]; 
    $lat = $tweet->coordinates->coordinates[1]; 
}