2013-02-21 94 views
0

經過對Gnip的幾項研究,我將它集成到我的應用程序中,並且我設法添加新規則並從Gnip獲取流結果,但是現在我在將其轉換爲PHP數組並顯示它針對PHP Array的Json解碼不會返回任何結果

我使用了CURLOPT_RETURNTRANSFER => TRUE,爲了不打印curl結果並將其存儲到一個變量中,並使用json_decode函數對其進行解碼,然後打印它但仍然獲得一個空白頁面。

如果有人能幫助我在此將提前

這裏我節省了很大的時間

感謝是我的代碼

$ch = curl_init(); 
    curl_setopt_array($ch, array(
     CURLOPT_URL => $stream_url, 
     CURLOPT_ENCODING => "gzip", 
     CURLOPT_FOLLOWLOCATION => true, 
     CURLOPT_HTTPAUTH => CURLAUTH_BASIC, 
     CURLOPT_USERPWD => $user.":".$pass, 
     CURLOPT_WRITEFUNCTION => "print_out_data", 
     CURLOPT_RETURNTRANSFER => TRUE, 
     CURLOPT_LOW_SPEED_TIME=> 60, 
     CURLOPT_TIMEOUT => 10, 

//CURLOPT_VERBOSE => true // uncomment for curl verbosity 

    )); 


    $content = curl_exec($ch); 

    $arr = json_decode($content, true); 
    $foo = $arr['id']; 
    $bar = $arr['objectType']; 

    print_r($foo); 
    print_r($bar); 

結果打印

當我得到
{"id":"tag:search.twitter.com,2005:304468052219662336","objectType":"activity","actor":{"objectType":"person","id":"id:twitter.com:348088144","link":"http://www.twitter.com/Devin_Giordan0","displayName":"ᎠƎϑᎥИ ᗩИ✞ℍ⚙ИУ","postedTime":"2011-08-03T21:21:01.000Z","image":"http://a0.twimg.com/profile_images/3271229767/481419170edf2c639441ce43cd540f0e_normal.jpeg","summary":"I will see a Million Before I diee\nInstagram-Youngnrecklessss\nFollow me and ill follow back \nFuck what people think, stay real. ","links":[{"href":null,"rel":"me"}],"friendsCount":476,"followersCount":265,"listedCount":0,"statusesCount":2773,"twitterTimeZone":null,"verified":false,"utcOffset":null,"preferredUsername":"Devin_Giordan0","languages":["en"],"location":{"objectType":"place","displayName":"Brooklyn, Ny"}},"verb":"share","postedTime":"2013-02-21T05:50:15.000Z","generator":{"displayName":"Echofon","link":"http://www.echofon.com/"},"provider":{"objectType":"service","displayName":"Twitter","link":"http://www.twitter.com"},"link":"http://twitter.com/Devin_Giordan0/statuses/304468052219662336","body":"RT @julianneromero1: All I wanna do is make youuu happy ","object":{"id":"tag:search.twitter.com,2005:304448192165076993","objectType":"activity","actor":{"objectType":"person","id":"id:twitter.com:383846395","link":"http://www.twitter.com/julianneromero1","displayName":"julieeeeee","postedTime":"2011-10-02T16:35:38.000Z","image":"http://a0.twimg.com/profile_images/3259175129/bdd9337b0e266c851a471f7034ca319a_normal.jpeg","summary":"Old enough to know better, young enough to not give a fuck. ✊","links":[{"href":null,"rel":"me"}],"friendsCount":409,"followersCount":483,"listedCount":0,"statusesCount":18986,"twitterTimeZone":"Quito","verified":false,"utcOffset":"-18000","preferredUsername":"julianneromero1","languages":["en"]},"verb":"post","postedTime":"2013-02-21T04:31:20.000Z","generator":{"displayName":"Twitter for iPhone","link":"http://twitter.com/download/iphone"},"provider":{"objectType":"service","displayName":"Twitter","link":"http://www.twitter.com"},"link":"http://twitter.com/julianneromero1/statuses/304448192165076993","body":"All I wanna do is make youuu happy ","object":{"objectType":"note","id":"object:search.twitter.com,2005:304448192165076993","summary":"All I wanna do is make youuu happy ","link":"http://twitter.com/julianneromero1/statuses/304448192165076993","postedTime":"2013-02-21T04:31:20.000Z"},"twitter_entities":{"hashtags":[],"urls":[],"user_mentions":[]}},"twitter_entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"julianneromero1","name":"julieeeeee","id":383846395,"id_str":"383846395","indices":[3,19]}]},"retweetCount":5,"gnip":{"matching_rules":[{"value":"Happy","tag":null}],"klout_score":42,"language":{"value":"en"}}} {"id":"tag:search.twitter.com,2005:304468052492300288","objectType":"activity","actor":{"objectType":"person","id":"id:twitter.com:1192574623","link":"http://www.twitter.com/Krystlegtsi","displayName":"Tracee Pavlik","postedTime":"2013-02-18T09:14:11.000Z","image":"http://a0.twimg.com/profile_images/3271765828/92aae4995378723671641594159a6ad7_normal.jpeg","summary":null,"links":[{"href":"http://tinyurl.com/b23r4ee","rel":"me"}],"friendsCount":20,"followersCount":0,"listedCount":0,"statusesCount":4,"twitterTimeZone":null,"verified":false,"utcOffset":null,"preferredUsername":"Krystlegtsi","languages":["en"]},"verb":"post","postedTime":"2013-02-21T05:50:15.000Z","generator":{"displayName":"web","link":"http://twitter.com"},"provider":{"objectType":"service","displayName":"Twitter","link":"http://www.twitter.com"},"link":"http://twitter.com/Krystlegtsi/statuses/304468052492300288","body":"It was an Amazing event tonight ...and makes me happy to see the president is educated speaks English Mashallah.","object":{"objectType":"note","id":"object:search.twitter.com,2005:304468052492300288","summary":"It was an Amazing event tonight ...and makes me happy to see the president is educated speaks English Mashallah.","link":"http://twitter.com/Krystlegtsi/statuses/304468052492300288","postedTime":"2013-02-21T05:50:15.000Z"},"twitter_entities":{"hashtags":[],"urls":[],"user_mentions":[]},"retweetCount":0,"gnip":{"matching_rules":[{"value":"Happy","tag":null}],"language":{"value":"en"}}} 
+1

你能分享一下$ content變量嗎? – Ankit 2013-02-21 05:41:11

+0

如果您在「json_decode」之前打印$內容,您會看到什麼?有沒有什麼迴應? – sanandrl 2013-02-21 05:43:00

+0

Ohh打印$內容時我不會得到任何結果但CURLOPT_RETURNTRANSFER => TRUE它會打印出我的json結果,並且現在將結果粘貼到我的問題選項卡 – doubleD 2013-02-21 05:49:52

回答

0
  1. che ck內容$content
  2. 如果有內容,請檢查(print_r($arr)),檢查結構並根據它進行打印。
+0

您好,感謝回覆,但問題是$ content是空的,不會打印任何東西。 – doubleD 2013-02-21 05:53:19

+0

@doubleD:當我刪除「CURLOPT_RETURNTRANSFER => TRUE」時檢查你的捲曲 – 2013-02-21 05:54:43

+0

然後它打印結果,並且將結果粘貼到我的問題中 – doubleD 2013-02-21 05:58:35

0

看來你在在rsult串

只應使用UTF-字符JSON編碼/解碼功能,具有非UTF字符顯示名