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?

2

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

6 Comments

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
use $array['productDetails'][0]['product'] instead of $jsonData->productDetails[0]->product in log..
json_decode($json,true) return associative array, associative array is printed with key to value like $array['key'] not $array->key
simply use echo $jsonData['productDetails'][0]['product'] and see what is printed...
It prints the complete json response. Shall we move to chat ?
|
2

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

3 Comments

I made that change, its still giving me nothing. Can you write the full statement to be used inside log
@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?
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
1
Mage::log( $jsonData->productDetails[0]->product); 

Or use associate array as mentioned.

4 Comments

still doesn't prints anything
@mayank When you say it "doesn't prints anything" what do you mean? It is a log method so it is most likely logging to a file instead of producing output. Use echo if you want output.
sorry for wrong wording, it logs nothing,( an empty value)
Use var_dump($jsonData->productDetails[0]->product)
-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>";

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.