【发布时间】:2017-09-21 01:57:25
【问题描述】:
您好,我正在尝试使用以下代码回显从我正在使用的 api 返回的数据:
<?php
ini_set("include_path", '/home/matthewt/php:' . ini_get("include_path") );
// This sample uses the Apache HTTP client from HTTP Components
(http://hc.apache.org/httpcomponents-client-ga/)
require_once 'HTTP/Request2.php';
$url = $request->getUrl();
$headers = array(
// Request headers
'Ocp-Apim-Subscription-Key' => 'my_id',
);
$request->setHeader($headers);
$parameters = array(
// Request parameters
);
$url->setQueryVariables($parameters);
$request->setMethod(HTTP_Request2::METHOD_GET);
// Request body
$request->setBody("{body}");
try
{
$response = $request->send();
echo $response->getBody();
}
catch (HttpException $ex)
{
echo $ex;
}
到目前为止,一切都很好,返回的 json 数据基本上如下所示:(我缩短了字段数量以节省空间)
[{"GameID":49810,"Season":2017},{"GameID":49811,"Season":2017}]
我需要知道的是如何遍历这些数据以打印出每场比赛的结果。我知道我必须使用这样的东西:
$arr = json_decode();
foreach($arr as $item) { //foreach element in $arr
$game_id = $item['GameID']; //etc
}
但我不确定需要解码什么...是 $response 吗?
谢谢
【问题讨论】: