Convert response to readable form
Question
I am posting request to paypal using curl.
Please check following code
$url = 'https://api-3t.sandbox.paypal.com/nvp';
$request = array();
$request['USER'] = 'username' ;
--- sending other request fields like above ---
$rd = '';
foreach ($request as $_key => $_val)
{
$rd .= $_key . '=' . urlencode(mb_convert_encoding($_val, 'ISO-8859-1', 'UTF-8')) . '&';
}
$output = array();
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, $url);
curl_setopt($curlSession, CURLOPT_HEADER, 0);
curl_setopt($curlSession, CURLOPT_POST, 1);
curl_setopt($curlSession, CURLOPT_POSTFIELDS, $rd);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlSession, CURLOPT_TIMEOUT, 90);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 1);
$rawresponse = curl_exec($curlSession);
Now when curl execution is performed I am getting below reposne.
TIMESTAMP=2015%2d07%2d23T10%3a39%3a36Z&CORRELATIONID=61f4332aaf13e&ACK=Success&VERSION=90&BUILD=17485293
My issue is how to convert this in readable array form ? Please help me
Solution
Let's say the variable $response
contains what you get from paypal.
$decoded = urldecode($response);
will get you
TIMESTAMP=2015-07-23T10:39:36Z&CORRELATIONID=61f4332aaf13e&ACK=Success&VERSION=90&BUILD=17485293
further calling
$split = explode('&', $decoded);
will get you
Array
(
[0] => TIMESTAMP=2015-07-23T10:39:36Z
[1] => CORRELATIONID=61f4332aaf13e
[2] => ACK=Success
[3] => VERSION=90
[4] => BUILD=17485293
)
You can even split further each element by =
and get a key value pair.
[Edit]
Or better yet
$arr = array();
parse_str(urldecode($response), $arr);
echo "<pre>"; print_r($arr);
will get you
Array
(
[TIMESTAMP] => 2015-07-23T10:39:36Z
[CORRELATIONID] => 61f4332aaf13e
[ACK] => Success
[VERSION] => 90
[BUILD] => 17485293
)
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange