2015-08-28 144 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] => 
        ) 

      ) 

    )