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?

图片转代码服务由CSDN问答提供 功能建议

对于数组输出,我将json decode设置为true .. 这是使用curl的以下响应格式: </ p>

  {
“process_time”:0.018849853,
“data”:[
 {
“id”:“recent_search”,
“name”:“  “,
”项目“:[] 
},
 {
”id“:”recent_view“,
”名称“:”“,
”项目“:[] 
},
  {
“id”:“popular_search”,
“name”:“”,
“items”:[] 
},
 {
“id”:“digital”,
“name”  :“DIGITAL”,
“items”:[] 
},
 {
“id”:“autocomplete”,
“name”:“AUTOCOMPLETE”,
“items”:[
 {  
“关键字”:“tas wanita batam”,
“url”:“/ search?q = tas + wanita + batam&amp; source = universe&amp; st = product”,
},
 {
“关键字 “:”tas wanita import“,
”url“:”/ search?q = tas + wanita + import&amp; source = universe&amp; st = product“,
},
 {
”关键字“:”tas  wanita murah“,
  “url”:“/ search?q = tas + wanita + murah&amp; source = universe&amp; st = product”,
},
 {
“keyword”:“tas wanita brandded”,
“url”:  “/ search?q = tas + wanita + brandded&amp; source = universe&amp; st = product”,
},
 {
“keyword”:“tas wanita fossil”,
“url”:“/ search?  q = tas + wanita + fossil&amp; source = universe&amp; st = product“,
} 
] 
} 
] 
} 
 </ code> </ pre> 
 
 

我的 PHP代码:</ p>

 &lt;?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));  
?&gt; 
 </ code> </ pre> 
 
 

我想仅在php变量中保存零件项“keyword”。</ p>

如何 我可以做吗? 什么是最好的方式呢?</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题