2012-10-31 51 views
0

我有以下使用Twitter API以JSON格式抓取的鳴叫數據:我想將它存儲在mysql或其他數據庫中以供進一步處理,我應該怎麼做才能將它存儲在某些關係型數據庫。將原始鳴叫數據轉換爲PHP關聯數組

{u'iso_language_code': u'en', 
'to_user_name': None, 
'to_user_id_str': u'0', 
'profile_image_url_https': u'https://si0.twimg.com/profile_images/1188223217/aoa-logo_normal.jpg', 
'from_user_id_str': u'225222094', 
'text': u'CoS: Live Review: Mission of Burma at Chicago\u2019s Lincoln Hall (9/29) http://t.co/cjeSqimv #axisofaudio', 
'from_user_name': u'Axis Of Audio', 
'profile_image_url': u'http://a0.twimg.com/profile_images/1188223217/aoa-logo_normal.jpg', 
'id': 252638989289398273L, 
'to_user': None, 
'source': u'<a href="http://dlvr.it">dlvr.it</a>', 
'id_str': u'252638989289398273', 
'from_user': u'axisofaudio', 
'from_user_id': 225222094, 
'to_user_id': 0, 
'geo': None, 
'created_at': u'Mon, 01 Oct 2012 05:20:03 +0000', 
'metadata': {u'result_type': u'recent'} 

問候

+0

您的JSON無效!你如何得到它?這個奇怪的'你'無處不在.. –

回答

0

要將其轉換成一個數組,您可以使用json_decode,簡單的例子:

<?php 
    $feed = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter&count=10&include_rts=t"; 
    $feedContent = file_get_contents($feed); 
    $feedContent = json_decode($feedContent, true); 
    echo '<pre>'; 
    print_r($feedContent); 
    echo '</pre>'; 

但是,你不能只是存儲在數據庫中的數組,因此無論是商店數據庫中的json數據,或者最好在將數組添加到數據庫之前將其存儲到數組中。

+0

他的json無效。 –

+0

我假設他有某個地方有效的JSON,或者至少從Twitter獲得有效的JSON。當他將JSON從某處複製到他的問題時可能只是有問題。 – Nick

+0

執行上面的代碼後,我得到以下錯誤:警告:file_get_contents()[function.file-get-contents]:php_network_getaddresses:getaddrinfo失敗:沒有這樣的主機是已知的。在第3行的C:\ wamp \ www \ me.php和警告:file_get_contents(http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter&count=10&include_rts=t)[function.file-獲取內容]:未能打開流:php_network_getaddresses:getaddrinfo失敗:沒有這樣的主機是已知的。在第3行的C:\ wamp \ www \ me.php中。 –