如何使用PHP获取JSON输出

时间:2015-04-28 22:21:59

标签: javascript php json

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

public function manage_categories() {

        $this->securePage();

        $this->preventAdmin();

        $this->load->library('pagination');

        $config['base_url'] = base_url('categories/manage-categories');

        $config['total_rows'] = $this->categories_model->getAllCategories()->num_rows();

        $config['uri_segment'] = 3;

        $config['per_page'] = $this->config->item('per_page');

        $this->pagination->initialize($config);

        $data['categories'] = $this->categories_model->getAllCategories($config['per_page'], $this->uri->segment(3));



        $data['news_title'] = 'Manage Categories | Kanchannew.com';

        $data['Keywords'] = 'Manage Categories';

        $data['url'] = '';

        $data['content'] = $this->load->view('categories/manage', $data, true);

        $this->load->view('kanchan', $data);
    }

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

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

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

虽然没有任何想法?

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

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

3 个答案:

答案 0 :(得分:2)

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

测试您的代码

答案 1 :(得分:0)

请看这个例子Parsing JSON object in PHP using json_decode

这是一种解析它的方法。     

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

答案 2 :(得分:0)

您可以将该JSON数据转换为可以轻松使用PHP的数组。为此,请使用PHP的requestAnimationFrame函数。

json_decode()

重要的是你传递$decodedJSON = json_decode($JSON, true); 作为第二个参数,否则你将无法获得阵列。

现在您有了一个数组,您可以从该数组中的JSON数据访问元素。例如:

true

在上面的示例中,$decodedJSON[key] 可以是我们转换的JSON数据中的任何键,它将返回属于该键的任何值。