douguai7291 2019-03-05 05:09
浏览 298
已采纳

PHP - 从curl响应中获取特定值

I have json decode set to true for array output.. This is the following format of response from using curl:

{
  "process_time": 0.018849853,
  "data": [
    {
      "id": "recent_search",
      "name": "",
      "items": []
    },
    {
      "id": "recent_view",
      "name": "",
      "items": []
    },
    {
      "id": "popular_search",
      "name": "",
      "items": []
    },
    {
      "id": "digital",
      "name": "DIGITAL",
      "items": []
    },
    {
      "id": "autocomplete",
      "name": "AUTOCOMPLETE",
      "items": [
        {
          "keyword": "tas wanita batam",
          "url": "/search?q=tas+wanita+batam&source=universe&st=product",
        },
        {
          "keyword": "tas wanita import",
          "url": "/search?q=tas+wanita+import&source=universe&st=product",
        },
        {
          "keyword": "tas wanita murah",
          "url": "/search?q=tas+wanita+murah&source=universe&st=product",
        },
        {
          "keyword": "tas wanita branded",
          "url": "/search?q=tas+wanita+branded&source=universe&st=product",
        },
        {
          "keyword": "tas wanita fossil",
          "url": "/search?q=tas+wanita+fossil&source=universe&st=product",
        }
      ]
    }
  ]
}

My PHP Code:

<?php
$url = 'hxxp://domain.com/univ/v8?q=tas+wanita';
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);

print_r(json_decode($r, true));
?>

I want to save the parts items "keyword" only in php variable..

How can I do it? And what would be the best way of doing it?

  • 写回答

1条回答 默认 最新

  • douguanya6399 2019-03-05 05:15
    关注
        <?php
            $url = 'hxxp://domain.com/univ/v8?q=tas+wanita';
            $ch=curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $r=curl_exec($ch);
            curl_close($ch);
    
            $data = json_decode($r, true);
            $i=0;
            foreach($data['data'] as $val) {
              foreach($val['items'] as $key => $item) { //it may give warning because empty array (i.e items = [].
                 $keywords[$i] = $item['keyword']; // this will store keyword in $keywords array.
                 $i++;
              }
            }
            $message = '<html><body>';
            $message .= '<table border="1" cellpadding="10">';
            $message .= "<tr><th><strong>Sr. No.:</strong> </th><th> 
                         <strong>Keyword</strong> </th></tr>";
            foreach ($keywords as $key => $value) {
               $message .= "<tr><td>".$key." </td><td>" .$value. "</td></tr>";
            }
            $message .= "</table>";
            $message .= "</body></html>";
            echo $message;
        ?>
    

    The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialised variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵