-1

I am trying to use bing search API and i got result in JSON format, now i want get particular node value from that JSON result like(url, displayurl, snippet etc).

This is the JSON result format i got

JSON Response:

{
  "_type": "SearchResponse",
  "queryContext": {
    "originalQuery": "infokart India Pvt. Ltd."
  },
  "webPages": {
    "webSearchUrl": "https://www.bing.com/search?q=infokart+India+Pvt.+Ltd.",
    "totalEstimatedMatches": 12200000,
    "value": [
      {
        "id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.0",
        "name": "Infokart India Pvt. Ltd.",
        "url": "http://infokartindia.com/",
        "isFamilyFriendly": true,
        "displayUrl": "infokartindia.com",
        "snippet": "Journals Subscription Services,International Subscription Agency,Experiences subscription agency",
        "dateLastCrawled": "2018-07-24T11:49:00.0000000Z",
        "language": "en"
      }
    ]
  },
  "rankingResponse": {
    "mainline": {
      "items": [
        {
          "answerType": "WebPages",
          "resultIndex": 0,
          "value": {
            "id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.0"
          }
        }
      ]
    }
  }
}

Please suggest!!

Nico
  • 6,259
  • 4
  • 24
  • 40
Harinarayan
  • 121
  • 1
  • 2
  • 12

1 Answers1

1
<?php 

$json = '{"_type":"SearchResponse","queryContext":{"originalQuery":"infokart India Pvt. Ltd."},"webPages":{"webSearchUrl":"https://www.bing.com/search?q=infokart+India+Pvt.+Ltd.","totalEstimatedMatches":12200000,"value":[{"id":"https://api.cognitive.microsoft.com/api/v7/#WebPages.0","name":"Infokart India Pvt. Ltd.","url":"http://infokartindia.com/","isFamilyFriendly":true,"displayUrl":"infokartindia.com","snippet":"Journals Subscription Services,International Subscription Agency,Experiences subscription agency","dateLastCrawled":"2018-07-24T11:49:00.0000000Z","language":"en"}]},"rankingResponse":{"mainline":{"items":[{"answerType":"WebPages","resultIndex":0,"value":{"id":"https://api.cognitive.microsoft.com/api/v7/#WebPages.0"}}]}}}';

$json_data = json_decode($json,true);

foreach($json_data['webPages']['value'] as $each_data){
    foreach($each_data as $each_key => $each_value){
        if(in_array(strtolower($each_key),['url','displayurl','snippet'])){
            echo $each_key," => ",$each_value,"<br/>";
        }
    }
}

OUTPUT

url => http://infokartindia.com/
displayUrl => infokartindia.com
snippet => Journals Subscription Services,International Subscription Agency,Experiences subscription agency
nice_dev
  • 17,053
  • 2
  • 21
  • 35