首頁  >  文章  >  後端開發  >  如何在 PHP 中從 Twitter 搜尋 API JSON 物件存取趨勢名稱?

如何在 PHP 中從 Twitter 搜尋 API JSON 物件存取趨勢名稱?

Barbara Streisand
Barbara Streisand原創
2024-10-19 11:33:30587瀏覽

How to Access Trend Names from a Twitter Search API JSON Object in PHP?

使用Twitter 趨勢示例訪問PHP JSON 對像中的數據

問題:

您已從Twitter 搜尋API 取得JSON 數據,並希望從JSON 檔案中提取趨勢名稱。 JSON 物件包含一個趨勢物件數組,每個趨勢物件都有一個「name」屬性。

解決方案:

要從 JSON 物件存取趨勢名稱,您可以使用循環遍歷趨勢物件陣列並列印每個物件的「name」屬性。

代碼:

<code class="php">$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl, 0, null, null);
$json_output = json_decode($json);

foreach ($json_output->trends as $trend) {
    echo "{$trend->name}\n";
}</code>

輸出:

Vote
Halloween
Starbucks
#flylady
#votereport
Election Day
#PubCon
#defrag08
Melbourne Cup
Cheney

以上是如何在 PHP 中從 Twitter 搜尋 API JSON 物件存取趨勢名稱?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn