的API请求一得到一个字符串的JSON服务器响应这样的:HTTP API请求Brandwatch PHP
{"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":"[email protected]","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":"[email protected]","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":"[email protected]","lockedQuery":false,"lockedByUsername":null}]}
所以我用json_decode得到一个数组。
现在我想解析阵列,因为我只需要“ID”:XXXXXXXX和“名” 我的代码是:
$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);
我会得到一个致命错误:不能使用stdClass类型的对象作为数组在/data/kunden/cylab/BH/produktion/web/htdocs_final/brandwatch/brandwatch.php在第31行。
我该怎么办?只看到数组的一个条目,我该如何解析它? 感谢您的帮助!
你能张贴上brandwatch.php的31行的代码? – GentlemanMax
请发送'echo'
';' – MonkeyZeus