androider
androider

Reputation: 992

Curl Response to php object

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?

Upvotes: 1

Views: 11761

Answers (4)

Juan Denis
Juan Denis

Reputation: 21

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>";

}

Upvotes: -1

Manjeet Barnala
Manjeet Barnala

Reputation: 2995

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

Upvotes: 2

MrGapo
MrGapo

Reputation: 348

Mage::log( $jsonData->productDetails[0]->product); 

Or use associate array as mentioned.

Upvotes: 1

Chris
Chris

Reputation: 5876

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

Upvotes: 2

Related Questions