1

I making this curl request:

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "affiliate-feeds.snapdeal.com/feed/api/order?startDate=2016-01-01&endDate=2016-05-03&status=approved");
    curl_setopt(
            $ch, CURLOPT_HTTPHEADER,
            array(
                    'Snapdeal-Affiliate-Id:'.$affiliateId,
                    'Snapdeal-Token-Id:'.$token,
                    'Accept:application/json'
            )
    );
    $response = curl_exec($ch);
    curl_close($ch);

    // work with $response here:

     $jsonData = json_decode($response);
     Mage::log( $jsonData["productDetails"][0]["product"]); 

The response is this:

{"productDetails":[{"product":"Philips QT4000 Trimmer Black","category":"Appliances","orderCode":"12569696012","quantity":1,"price":936.0,"sale":936.0,"commissionRate":1.0,"commissionEarned":9.36,"dateTime":"03/29/2016 22:49:06","affiliateSubId1":"","affiliateSubId2":"null","userType":"EXISTING","deviceType":"web"}],"nextURL":null}

The log statement prints nothing. What I am doing wrong here?

androider
  • 982
  • 6
  • 16
  • 32
  • Possible duplicate of [get data from return value of mandrillapp array](http://stackoverflow.com/questions/37018951/get-data-from-return-value-of-mandrillapp-array) – Manjeet Barnala May 04 '16 at 10:28
  • [VISIT](http://stackoverflow.com/a/37019007/4018240) this, use `json_decode($response,true)` it will return a associative array... – Manjeet Barnala May 04 '16 at 10:30

4 Answers4

2

json_decode decodes to an object by default. Do $jsonData = json_decode($response, true); if you want an associative array.

Chris
  • 5,571
  • 2
  • 20
  • 32
  • I made that change, its still giving me nothing. Can you write the full statement to be used inside log – androider May 04 '16 at 10:35
  • @mayank You don't need to change the statement inside the call to `log`. I've just tested this with the JSON you provided and it works fine. Are you sure you are looking in the correct file for the log output? – Chris May 04 '16 at 10:45
  • You are using the json directly, maybe thats the issue. Maybe, getting the curl response and then using json_decode on it seems to be the trouble. Maybe I have to convert the curl response to something – androider May 04 '16 at 10:47
2

Use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); in your curl and pass true with json_decode()....

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "affiliate-feeds.snapdeal.com/feed/api/order?startDate=2016-01-01&endDate=2016-05-03&status=approved");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt(
            $ch, CURLOPT_HTTPHEADER,
            array(
                    'Snapdeal-Affiliate-Id:'.$affiliateId,
                    'Snapdeal-Token-Id:'.$token,
                    'Accept:application/json'
            )
    );
    $response = curl_exec($ch);
    curl_close($ch);

    // work with $response here:
    $jsonData = json_decode($response,true);
     Mage::log($jsonData['productDetails'][0]['product']); 

This will output :

Philips QT4000 Trimmer Black
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
  • I made that change: Here it is: $response = curl_exec($ch); curl_close($ch); // work with $response here: $jsonData = json_decode($response,true); Mage::log( $jsonData["productDetails"][0]["product"],null,"mylogfile1.log",true); still prints nothing – androider May 04 '16 at 10:38
  • use `$array['productDetails'][0]['product']` instead of `$jsonData->productDetails[0]->product` in log.. – Manjeet Barnala May 04 '16 at 10:39
  • `json_decode($json,true)` return associative array, associative array is printed with key to value like `$array['key']` not `$array->key` – Manjeet Barnala May 04 '16 at 10:41
  • simply use `echo $jsonData['productDetails'][0]['product']` and see what is printed... – Manjeet Barnala May 04 '16 at 10:44
  • It prints the complete json response. Shall we move to chat ? – androider May 04 '16 at 10:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110985/discussion-between-manjeet-barnala-and-mayank). – Manjeet Barnala May 04 '16 at 10:54
1
Mage::log( $jsonData->productDetails[0]->product); 

Or use associate array as mentioned.

dbf
  • 3,278
  • 1
  • 24
  • 34
MrGapo
  • 348
  • 3
  • 10
-1

Here is how you make a curl call and get the content from that website

<?php
if (!function_exists('curl_version')) {
    exit("Enable cURL in PHP");
}

$url = "https://www.google.com/";

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_POSTFIELDS => "",
    CURLOPT_HTTPHEADER => array(
        "Accept: */*",
        "Cache-Control: no-cache",
        "Connection: keep-alive",
        "Host: " . url($url),
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
        "accept-encoding: gzip, deflate",
        "cache-control: no-cache",
    ),
));

function url($url)
{
    $result = parse_url($url);
    return $result['host'];
}
$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo "<textarea>" . $response . "</textarea>";

}
Juan Denis
  • 21
  • 7