gpt4 book ai didi

javascript - 如何使用 PHP 获取 JSON 输出

转载 作者:行者123 更新时间:2023-11-29 21:49:31 25 4
gpt4 key购买 nike

好的,这是我第一次尝试使用 TwitchAPI。当我发出请求时,我得到:

{"follows":[{"created_at":"2015-04-28T01:04:33Z","_links":{"self":"https://api.twitch.tv/kraken/users/chaoticaura/follows/channels/giygaslp"},"notifications":true,"user":{"_id":54441701,"name":"chaoticaura","created_at":"2014-01-05T01:06:19Z","updated_at":"2015-04-28T14:18:50Z","_links":{"self":"https://api.twitch.tv/kraken/users/chaoticaura"},"display_name":"ChaoticAura","logo":"http://static-cdn.jtvnw.net/jtv_user_pictures/chaoticaura-profile_image-3b6a888d174153f6-300x300.jpeg","bio":"Welcome to the House of Gaming/Crazy/Stupid ChaoticAura","type":"user"}},{"created_at":"2014-08-10T06:25:10Z","_links":{"self":"https://api.twitch.tv/kraken/users/phoenix089/follows/channels/giygaslp"},"notifications":true,"user":{"_id":31004257,"name":"phoenix089","created_at":"2012-06-03T01:41:37Z","updated_at":"2015-04-22T19:58:28Z","_links":{"self":"https://api.twitch.tv/kraken/users/phoenix089"},"display_name":"phoenix089","logo":null,"bio":null,"type":"user"}},{"created_at":"2014-05-10T17:41:05Z","_links":{"self":"https://api.twitch.tv/kraken/users/monanniverse/follows/channels/giygaslp"},"notifications":true,"user":{"_id":30041264,"name":"monanniverse","created_at":"2012-04-25T10:45:21Z","updated_at":"2015-04-17T18:58:05Z","_links":{"self":"https://api.twitch.tv/kraken/users/monanniverse"},"display_name":"Monanniverse","logo":null,"bio":null,"type":"user"}},{"created_at":"2013-04-25T01:10:57Z","_links":{"self":"https://api.twitch.tv/kraken/users/princess_sarahkat/follows/channels/giygaslp"},"notifications":true,"user":{"_id":27411850,"name":"princess_sarahkat","created_at":"2012-01-13T23:45:04Z","updated_at":"2014-08-01T17:49:47Z","_links":{"self":"https://api.twitch.tv/kraken/users/princess_sarahkat"},"display_name":"Princess_SarahKat","logo":"http://static-cdn.jtvnw.net/jtv_user_pictures/princess_sarahkat-profile_image-5b554c88c6eb89a9-300x300.png","bio":null,"type":"user"}},{"created_at":"2012-12-04T13:43:15Z","_links":{"self":"https://api.twitch.tv/kraken/users/thedaredevil717/follows/channels/giygaslp"},"notifications":true,"user":{"_id":38212339,"name":"thedaredevil717","created_at":"2012-12-04T13:41:17Z","updated_at":"2013-09-27T12:38:53Z","_links":{"self":"https://api.twitch.tv/kraken/users/thedaredevil717"},"display_name":"Thedaredevil717","logo":null,"bio":null,"type":"user"}}],"_total":5,"_links":{"self":"https://api.twitch.tv/kraken/channels/giygaslp/follows?direction=DESC&limit=25&offset=0","next":"https://api.twitch.tv/kraken/channels/giygaslp/follows?direction=DESC&limit=25&offset=25"}}

有人告诉我这是一个 JSON 响应。我如何获取此信息,并将其与 PHP 中的变量一起使用?

我做了一些失败的尝试,这是代码:

<html>
<?php $json=json_decode(file_get_contents( "https://api.twitch.tv/kraken/channels/giygaslp/follows?limit=1")); $currentFollower=0 ; $currentPage=0 ; $resultsPerPage=5 ; $tableHtml=<
<<TABLE <div id="page-number-%s" style="%s">
<table>
<tr>
<th>Username:</th>
<th>Follow Date:</th>
<th>Type:</th>
</tr>
%s
</table>
</div>
TABLE; $rowHtml =
<<<ROW <tr>
<td><a href="%s">%s</a>
</td>
<td>%s</td>
<td>%s</td>
</tr>
ROW; $html = ""; $rows = ""; foreach ($json->follows as $follow) { if ($currentFollower % $resultsPerPage == 0 && $currentFollower> 0) { $style = $currentPage === 0 ? '' : 'display:none'; $html .= sprintf($tableHtml, $currentPage, $style, $rows); $rows
= ""; $currentPage++; } $rows .= sprintf( $rowHtml, $follow->user->_links->self, $follow->user->name . ' (' . $currentFollower . ')', $follow->user->created_at, $follow->user->type ); $currentFollower++; } $html .=
<<<BUTTONS <button onclick="previousPage()">previous</button>
<button onclick="nextPage()">next</button>

BUTTONS; $javascript =
<<<JS <script>
var currentPage = 0; function previousPage() { if(currentPage > 0) { document.getElementById('page-number-'+currentPage).style.display = 'none'; currentPage--; document.getElementById('page-number-'+currentPage).style.display = ''; } }; function nextPage()
{ if(currentPage
< {$currentPage} - 1) { document.getElementById( 'page-number-'+currentPage).style.display='none' ; currentPage++; document.getElementById( 'page-number-'+currentPage).style.display='' ; } }; </script>
JS; echo $javascript.$html; ?>

</html>

但这行不通……有什么想法吗?

编辑:我目前正在使用它进行测试

<html>
<script>
<? php
$json = json_decode(file_get_contents("https://api.twitch.tv/kraken/channels/giygaslp/follows?limit=25"), true);
print $json['follows'];
var_dump($json['follows']) ?>
</script>

</html>

最佳答案

PHP 的 json_decode 有点困惑。它返回 stdClass .添加 true 选项以获得常规 PHP associative array .

$json = json_decode(file_get_contents( "http://myurl.com"), true);

var_dump($json['follows']); //var_dump is print for arrays


我发现有时在使用 stdClasses 时,您必须将键用作字符串:

$json->{'follows'}


PhpFiddle 测试您的代码

关于javascript - 如何使用 PHP 获取 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29930994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com