的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