Tony Stark Tony Stark - 5 months ago 17
PHP Question

JSON/cURL - Trying to decode results from GET request

Here is my code

(CURL)
:

<?php
$url = "http://www.sportsdirect.com/DesktopModules/ProductDetail/API/ItemSuggestions/GetProductSuggestions?productId=433006&numPlacements=1&ItemType=product";
// Initiate curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIE, "ChosenSite=www; SportsDirect_AnonymousUserCurrency=GBP; language=en-GB");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_VERBOSE, true);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
$collections = json_decode($result);

foreach ($collections as $item) {
$Title = $item->Products->Title;

echo $Title;
}


With this code i receive blank screen, no results are printed.
All i want is to
echo
all the
Title
names of all products.

Where is my mistake, how can i achieve what i want ?

Answer

Use Below code. It will help you definatly.

foreach ($collections as $row){
foreach ($row->Products as $item) {
    $Title = $item->Title;            

    echo $Title;
}

}

Comments