【发布时间】:2015-08-28 14:43:06
【问题描述】:
通过 API 请求,在 JSOn 中获取服务器响应,就像这样在一个字符串中:
{"resultsTotal":3,"resultsPage":-1,"resultsPageSize":-1,"results":[{"id":1998425622,"name":"Regionale Mentions_Branche","type":"search string","creationDate":"2015-08-21T15:13:58.226+0000","lastModificationDate":"2015-08-21T15:13:58.226+0000","lastModifiedUsername":"mueller@cybay.de","lockedQuery":false,"lockedByUsername":null},{"id":1998422533,"name":"HTP_Sponsoring","type":"search string","creationDate":"2015-08-18T08:53:38.136+0000","lastModificationDate":"2015-08-18T08:53:38.136+0000","lastModifiedUsername":"mueller@cybay.de","lockedQuery":false,"lockedByUsername":null},{"id":1998422529,"name":"HTP_Brand Mentions","type":"search string","creationDate":"2015-08-18T08:41:32.699+0000","lastModificationDate":"2015-08-18T14:42:19.977+0000","lastModifiedUsername":"mueller@cybay.de","lockedQuery":false,"lockedByUsername":null}]}
所以我使用 json_decode 来获取一个数组。
现在我想解析数组,因为我只需要 "id":xxxxxxxx 和 "name" 我的代码是:
$webservice = 'http://newapi.brandwatch.com/projects/';
$kundenId = $_POST["kunden"];
$key = "?access_token=XXXXXXXXX";
$onlySdate = $_POST["startdate"];
$onlyEdate = $_POST["enddate"];
$startdate = "&startDate=".$onlySdate ;
$enddate = "?endDate=" .$onlyEdate ;
$url = $webservice . $kundenId . "/queries/summary".$key;
$domainRequest = $url;
//header("Location:$domainRequest");
$data = array();
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
'Authorization: Basic ' . BASIC_AUTH,
'method' => 'GET',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($domainRequest, false, $context);
$array = json_decode($result, true);
//echo count ($result);
//echo "<br>";
print_r ($array);
如果我只想接收数组的一个条目,我在json_decode ($result); 中没有响应或没有 true,我将收到一个致命错误:无法在 /data/kunden/cylab/ 中使用 stdClass 类型的对象作为数组BH/produktion/web/htdocs_final/brandwatch/brandwatch.php 在第 31 行。
我能做什么?只查看数组的一个条目,我该如何解析它? 感谢您的帮助!
【问题讨论】:
-
你能把brandwatch.php第31行的代码贴出来吗?
-
请发布
echo '<pre>'.print_r($array, true).'</pre>';的输出
标签: php arrays api httprequest