Reputation: 41
Ok, this is my first try with TwitchAPI. When I make the request, I 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 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"}}
I'm told that this is a JSON Response. How do I take this information, and use it with variables in PHP?
I've made some attempts that failed, here is the 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 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>
That doesn't work though... any ideas?
Edit: Im using this at the moment for 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>
Upvotes: 2
Views: 199
Reputation: 14361
PHP's json_decode
is a bit confusing. It returns an stdClass. Add the true
option to get a regular PHP associative array.
$json = json_decode(file_get_contents( "http://myurl.com"), true);
var_dump($json['follows']); //var_dump is print for arrays
$json->{'follows'}
Upvotes: 3
Reputation: 1567
You can convert that JSON data to an array which you can easily use with PHP. To do this use PHP's json_decode()
function.
$decodedJSON = json_decode($JSON, true);
It's important you pass in true
as the second parameter otherwise you will not get an array back.
Now that you have an array you can access elements from that JSON data in that array. For example:
$decodedJSON[key]
In the above example, key
can be any key that was in the JSON data we converted and it will return whatever value belongs to that key.
Upvotes: 0
Reputation: 291
Look at this example Parsing JSON object in PHP using json_decode
and here is a way to parse it.
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
Upvotes: 0