javascript - How do I grab JSON output with PHP -


ok, first try twitchapi. when make request, get:

{"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 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"}} 

i'm told json response. how take information, , use variables in php?

i've made attempts failed, here code:

<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 $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> 

that doesn't work though... ideas?

edit: im using @ moment testing

<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's json_decode bit confusing. returns stdclass. add true option regular php associative array.

$json = json_decode(file_get_contents( "http://myurl.com"), true);  var_dump($json['follows']); //var_dump print arrays 


i've found when using stdclasses, have use key string:

$json->{'follows'} 


test code @ phpfiddle


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -