dongzhu0327 2015-08-28 14:43
浏览 22
已采纳

Brandwatch php的HTTP API请求

With an API Request a get a Server response in JSOn like this in one String:

{"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}]}

so I use the json_decode to get an array.

now i want to parse the array because i only need the "id":xxxxxxxx and the "name" my code is:

$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
" .
                                '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);

if i want to receive just one entry of the array i get no response or without the true in json_decode ($result); i'll get a Fatal error: Cannot use object of type stdClass as array in /data/kunden/cylab/BH/produktion/web/htdocs_final/brandwatch/brandwatch.php on line 31.

What can i do? to see just one entry of the array and how can i parse it? thanks for help!

  • 写回答

2条回答 默认 最新

  • douluyezhen4512 2015-08-28 14:57
    关注

    If you are really getting that json code from the request and you decode it with

    $array = json_decode($result, true);
    

    you will get an (associative) array that has the same structure as the json string (without the true value, you will get objects, that's why you got the Fatal Error). In that case, you can just access its fields, like so:

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

    yielding the output:

    id=1998425622, name=Regionale Mentions_Branche
    id=1998422533, name=HTP_Sponsoring
    id=1998422529, name=HTP_Brand Mentions
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?