I get a response via my localhost Wampserver from Paypal mobile SDK. I receive a string via a cURL call. This string has been verified to be in json form via the online service Json Lint website. I was able to convert the json string to an array in php code, using an online php compiler, phptester.net using php version 5.5. However, when i tried the same code on my wampserver it says that the array size equals zero. I don't know what could be wrong. Please help me evaluate my PHP code.
Here's the response from Paypal mobile sdk:
{"scope":"https://uri.paypal.com/services/subscriptions https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.*","nonce":"2016-03-06T22:13:44ZNxK118Mc40QthTeJX8Y_enneSe0y62kEo6mu0ICCwMU","access_token":"A101.ONfkNw-H4-VcjWL9ZUbLlaDj1YUApO7X9fhOslMKRLyW1JpKL1hLlD5W2m9xri-y.ZRF-23gpYMDQ5vWPkOsEiv8WbJC","token_type":"Bearer","app_id":"APP-80W284485P519543T","expires_in":32399}
Here's the relevant code i use in my php file:
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_USERPWD, $idclient.":".$sekret);
$result = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
echo $result;
$arr = (array) json_decode($result);
$token = $arr['access_token'];
echo "here's the token oken";
var_dump($arr);
var_dump($token);
Here's the code i use in phptester.net:
$e ='I copied and paste the response i get from server here. noticed the
single quotes here. Maybe this is the problem';
$a = (array)json_decode($e);
$b = $a['access_token'];
var_dump($b);
this code accurately prints out the value of the array at "access_token', which is the access_token i need to verify payments made to PayPal.
I need to replicate this good code on my Wamp server php file. But it is not doing what is supposed to. It says the size of my array is zero, and that the variable $token is null. Please help me in evaluating my code.
Thank you
Try this, don't try to cast json_decode
to array. you can use second option in json_decode
to convert it to associative arrays. PHP Doc
$e ='{"scope":"https://uri.paypal.com/services/subscriptions https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://uri.paypal.com/services/applications/webhooks openid https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.*","nonce":"2016-03-06T22:13:44ZNxK118Mc40QthTeJX8Y_enneSe0y62kEo6mu0ICCwMU","access_token":"A101.ONfkNw-H4-VcjWL9ZUbLlaDj1YUApO7X9fhOslMKRLyW1JpKL1hLlD5W2m9xri-y.ZRF-23gpYMDQ5vWPkOsEiv8WbJC","token_type":"Bearer","app_id":"APP-80W284485P519543T","expires_in":32399}';
$a = json_decode($e,true);
$b = $a['access_token'];
var_dump($b);
here
I was able to find the answer to my problem. I just had to get rid of the header which was returned in the returned string from PayPal. I did this by setting the following cURL option to false: curl_setopt($ch,CURLOPT_HEADER,true)
Then, I was able to use json_decode() on the returned string to convert it to an array, which allowed me to access the access_token index field in the array.
Now, I just need to store this string on my online database for purchase verification via PayPal mobile SDK.