Parsing the PHP response

Posted by anu31221@gmail.com on 14-Nov-2014 08:57

Hi

Need help 

I have a PHP code which respond with below. How to parse this response in Trigger (Object Script).

Response has multiple rows and each has set of value.

  1. [{"accountID":"5813","date":"2014-11-13 20:58:30","leadID":"5","ip":"64.39.152.175","page":"http:\/\/demo.idxbroker.com\/idx\/search.php?page=basic","referrer":"http:\/\/demo.idxbroker.com\/idx\/widgetpreview.php?widgetid=46897"},{"accountID":"5813","date":"2014-11-14 01:53:24","leadID":"5","ip":"64.39.152.175","page":"http:\/\/demo.idxbroker.com\/idx\/myaccount.php?vlog=1","referrer":"http:\/\/demo.idxbroker.com\/idx\/thankyou.php?action=verify&email=jadaru777@yahoo.com&code=4LHMtwRsmsZTO0tIoxOUSCFRFH1MweDw"}]

PHP code

/ set up cURL
$handle = curl_init();
//echo "how are traffic";
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

// exec the cURL request and returned information. Store the returned HTTP code in $code for later reference
$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

if ($code >= 200 || $code < 300)
$properties = json_decode($response,true);
else
$error = $code;

All Replies

Posted by Godfrey Sorita on 14-Nov-2014 09:27

Hi Anu,

This is a JSON response and you can learn how to parse/read it from this link.

The response you got is actually like:

[
    {
        "accountID": "5813",
        "date": "2014-11-13 20:58:30",
        "leadID": "5",
        "ip": "64.39.152.175",
        "page": "http://demo.idxbroker.com/idx/search.php?page=basic",
        "referrer": "http://demo.idxbroker.com/idx/widgetpreview.php?widgetid=46897"
    },
    {
        "accountID": "5813",
        "date": "2014-11-14 01:53:24",
        "leadID": "5",
        "ip": "64.39.152.175",
        "page": "http://demo.idxbroker.com/idx/myaccount.php?vlog=1",
        "referrer": "http://demo.idxbroker.com/idx/thankyou.php?action=verify&email=jadaru777@yahoo.com&code=4LHMtwRsmsZTO0tIoxOUSCFRFH1MweDw"
    }
]

And I was able to parse it from an Object Script using the code below:

var response_str = '[{"accountID":"5813","date":"2014-11-13 20:58:30","leadID":"5","ip":"64.39.152.175","page":"http:\/\/demo.idxbroker.com\/idx\/search.php?page=basic","referrer":"http:\/\/demo.idxbroker.com\/idx\/widgetpreview.php?widgetid=46897"},{"accountID":"5813","date":"2014-11-14 01:53:24","leadID":"5","ip":"64.39.152.175","page":"http:\/\/demo.idxbroker.com\/idx\/myaccount.php?vlog=1","referrer":"http:\/\/demo.idxbroker.com\/idx\/thankyou.php?action=verify&email=jadaru777@yahoo.com&code=4LHMtwRsmsZTO0tIoxOUSCFRFH1MweDw"}]';
var obj = JSON.parse(response_str);

rbv_api.print(obj[1]['accountID']); //Read the 2nd record's accountID field


Regards,

Godfrey

This thread is closed