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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码