2015-08-28 105 views
0

的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行。

我該怎麼辦?只看到數組的一個條目,我該如何解析它? 感謝您的幫助!

+1

你能張貼上brandwatch.php的31行的代碼? – GentlemanMax

+0

請發送'echo'

'.print_r($array, true).'
';' – MonkeyZeus

回答

0

響應如果你真的得到來自請求JSON代碼和你

$array = json_decode($result, true); 

解碼它,你將得到一個(關聯)陣列具有相同結構作爲json字符串(沒有true的值,您將獲得對象,這就是爲什麼您會遇到致命錯誤)。在這種情況下,你可以訪問它的字段,就像這樣:

foreach ($array["results"] as $result) { 
    echo "id=" . $result["id"] . ", name=" . $result["name"] . "\n"; 
} 

產生輸出:

id=1998425622, name=Regionale Mentions_Branche 
id=1998422533, name=HTP_Sponsoring 
id=1998422529, name=HTP_Brand Mentions 
+0

的輸出謝謝,它的工作原理! –

0

echo '<pre>'.print_r($array, true).'</pre>';

stdClass Object 
    (
     [resultsTotal] => 3 
     [resultsPage] => -1 
     [resultsPageSize] => -1 
     [results] => Array 
      (
       [0] => stdClass Object 
        (
         [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] => 
         [lockedByUsername] => 
        ) 

       [1] => stdClass Object 
        (
         [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] => 
         [lockedByUsername] => 
        ) 

       [2] => stdClass Object 
        (
         [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] => 
         [lockedByUsername] => 
        ) 

      ) 

    )