1

How to get Deeplinks in JSON format with PHP?

This is the JSON object I actually have

{
    "_type": "SearchResponse",
    "webPages": {
        "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=sFk5GtkV8PxaqVq0K3GniaQOQJqKR2jqw1udftLC9QE&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dcontemporary%2bfurniture&p=DevEx,5336.1",
        "totalEstimatedMatches": 46000000,
        "value": [{
            "id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.0",
            "name": "Contemporary Furniture. Modern and Scandinavian designs.",
            "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=Q0wwocl-JR44YrK1RsR_5rSqO4KPlSPx0KgUi-sMocA&v=1&r=https%3a%2f%2fcontemporaryfurniture.com%2f&p=DevEx,5080.1",
            "displayUrl": "https:\/\/contemporaryfurniture.com",
            "snippet": "The finest in contemporary and modern furniture. Imported and domestic styles, bedrooms, sofas, home office, more. Shop online or visit our NY showroom.",
            "deepLinks": [{
                "name": "Bedrooms",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=jo1rIVengiXnaKOkFczZ54qrp9-L4W2jb1sJ41jl04g&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fbedrooms-19%2f&p=DevEx,5072.1",
                "snippet": "Bedroom sets including beds, dressers, nightstands and chests. Produced in various woods and lacquers. Contemporary, scandinavian and modern designs."
            }, {
                "name": "Dining Rooms",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=EsyLPbUccu5BzwCeceP_S5u46vo4gSDMG7vP29BfjFE&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fdining-rooms-12%2f&p=DevEx,5073.1",
                "snippet": "Dining Room sets, dining tables, servers, buffet and china cabinets. Contemporary and Scandinavian styles."
            }, {
                "name": "Barstool",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=6rOwsJH4uT4LzjG833eSRYQgDMCKiogHGdgnokFVSEg&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fbarstool-16%2f&p=DevEx,5074.1",
                "snippet": "Barstools and counterstools contemporary, modern and scandinavian designs produced in wood and metal."
            }, {
                "name": "Chairs",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=ADlw0i43aQhdLC0tHBfjMFjYwiWYHZ3ggdG6RhZv9ss&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fchairs-13%2f&p=DevEx,5075.1",
                "snippet": "Chairs to compliment your living room produced in fabric upholstery and leather. Contemporary and modern designs."
            }],
            "dateLastCrawled": "2017-03-31T17:25:00"
        }, {
            "id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.1",
            "name": "Contemporary Furniture | Houzz",
            "url": "http:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=vsM0va_8kehN5ZnPil2frs3eKL16FOHnut88FvEHRsM&v=1&r=http%3a%2f%2fwww.houzz.com%2fphotos%2fcontemporary%2ffurniture&p=DevEx,5098.1",
            "displayUrl": "www.houzz.com\/photos\/contemporary\/furniture",
            "snippet": "Browse a wide selection of contemporary furniture for sale on Houzz, including wood furniture and leather furniture for your dining room, office, bedroom and living room.",
            "dateLastCrawled": "2017-03-30T22:51:00"
        }, {
            "id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.2",
            "name": "Affordable Modern Furniture, Decor & Lighting online. Free ...",
            "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=OwOQI48E8ZhxF8xWw-iB2POJliBV0LIE3CucLePUzeg&v=1&r=https%3a%2f%2fwww.contemporaryfurniturewarehouse.com%2f&p=DevEx,5112.1",
            "displayUrl": "https:\/\/www.contemporaryfurniturewarehouse.com",
            "snippet": "Modern & Contemporary Furniture on sale. Shop for sofas, sectionals, accent chairs, dining chairs, bar stools, office chairs, stools, outdoor furniture & more.",
            "dateLastCrawled": "2017-03-31T02:54:00"
        }

And here is the code I did so far:

    $i=1;       
    foreach($data->webPages->value as $val){
        if($i==1){
            $part   = $val['deepLinks']->snippet;
        }   
    $i++;       
    }

Results in: Fatal error: Cannot use object of type stdClass as array.

how to get that value?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
sugi putra
  • 25
  • 6
  • This question would be better if you edited it to include a sample of the JSON you're trying to parse through. Otherwise we can only guess. – Matt Apr 04 '17 at 17:47
  • i trying to edited code but still error json code – sugi putra Apr 04 '17 at 17:52
  • Add a JSON example that you're working with to your question. We are not magicians! – Matt Apr 04 '17 at 17:52
  • @mkaatman that the code i can't edit proper format here – sugi putra Apr 04 '17 at 18:02
  • 1
    Possible duplicate of [How do I extract data from JSON with PHP?](http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – miken32 Apr 05 '17 at 00:01
  • It literally says what's wrong `Fatal error: Cannot use object of type stdClass as array.` use `$val->deepLinks` instead accesing it as array – Peter Apr 07 '17 at 08:10

2 Answers2

0

I think it may have to do with $data. It isn't an Array.

Did you use json_decode() to turn your JSON object to a PHP variable? If yes, then you need to change it to:

 json_decode($myJSON, true);

If set to true PHP will return an associative array but If not PHP it will return a object(stdClass).

You can read more about why that is here

FrenchMajesty
  • 1,101
  • 2
  • 14
  • 29
0

If you want a flat array of deepLinks, try this:

$ret = [];
foreach($data->webPages->value as $val){
    if(property_exists($val,"deepLinks")){
        $ret = array_merge($ret, $val->deepLinks);
    }
}
var_dump($ret);

I tested it and it works.

On the other hand, if you want to get it grouped:

$ret = [];
foreach($data->webPages->value as $val){
    if(property_exists($val,"deepLinks")){
        $ret[] = $val->deepLinks;
    }
}
var_dump($ret);

The test JSON I've used:

$data = '{
    "_type": "SearchResponse",
    "webPages": {
        "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=sFk5GtkV8PxaqVq0K3GniaQOQJqKR2jqw1udftLC9QE&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dcontemporary%2bfurniture&p=DevEx,5336.1",
        "totalEstimatedMatches": 46000000,
        "value": [{
            "id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.0",
            "name": "Contemporary Furniture. Modern and Scandinavian designs.",
            "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=Q0wwocl-JR44YrK1RsR_5rSqO4KPlSPx0KgUi-sMocA&v=1&r=https%3a%2f%2fcontemporaryfurniture.com%2f&p=DevEx,5080.1",
            "displayUrl": "https:\/\/contemporaryfurniture.com",
            "snippet": "The finest in contemporary and modern furniture. Imported and domestic styles, bedrooms, sofas, home office, more. Shop online or visit our NY showroom.",
            "deepLinks": [{
                "name": "Bedrooms",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=jo1rIVengiXnaKOkFczZ54qrp9-L4W2jb1sJ41jl04g&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fbedrooms-19%2f&p=DevEx,5072.1",
                "snippet": "Bedroom sets including beds, dressers, nightstands and chests. Produced in various woods and lacquers. Contemporary, scandinavian and modern designs."
            }, {
                "name": "Dining Rooms",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=EsyLPbUccu5BzwCeceP_S5u46vo4gSDMG7vP29BfjFE&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fdining-rooms-12%2f&p=DevEx,5073.1",
                "snippet": "Dining Room sets, dining tables, servers, buffet and china cabinets. Contemporary and Scandinavian styles."
            }, {
                "name": "Barstool",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=6rOwsJH4uT4LzjG833eSRYQgDMCKiogHGdgnokFVSEg&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fbarstool-16%2f&p=DevEx,5074.1",
                "snippet": "Barstools and counterstools contemporary, modern and scandinavian designs produced in wood and metal."
            }, {
                "name": "Chairs",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=ADlw0i43aQhdLC0tHBfjMFjYwiWYHZ3ggdG6RhZv9ss&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fchairs-13%2f&p=DevEx,5075.1",
                "snippet": "Chairs to compliment your living room produced in fabric upholstery and leather. Contemporary and modern designs."
            }],
            "dateLastCrawled": "2017-03-31T17:25:00"
        }, {
            "id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.1",
            "name": "Contemporary Furniture | Houzz",
            "url": "http:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=vsM0va_8kehN5ZnPil2frs3eKL16FOHnut88FvEHRsM&v=1&r=http%3a%2f%2fwww.houzz.com%2fphotos%2fcontemporary%2ffurniture&p=DevEx,5098.1",
            "displayUrl": "www.houzz.com\/photos\/contemporary\/furniture",
            "snippet": "Browse a wide selection of contemporary furniture for sale on Houzz, including wood furniture and leather furniture for your dining room, office, bedroom and living room.",
            "dateLastCrawled": "2017-03-30T22:51:00"
        }]
    }
}';

I completed the missing parts.

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78