cURL调用数组我通过我的本地主机Wampserver从贝宝移动SDK获得响应。我通过cURL调用收到一个字符串。该字符串已通过在线服务Json Lint网站验证为json形式。我能够将json字符串转换为php代码中的数组,使用php在线编译器phptester.net使用php版本5.5。但是,当我在我的wampserver上尝试相同的代码时,它说数组大小等于零。我不知道什么可能是错的。请帮我评估我的PHP代码。从字符串导致在使用PHP
下面是来自PayPal手机支付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}
下面是相关的代码在我的PHP文件中使用:
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);
这是我在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);
该代码准确地打印出“access_token”数组的值,这是我需要的access_token验证向PayPal付款。
我需要在我的Wamp服务器php文件中复制这个好代码。但它没有做到应该做的。它说我的数组的大小为零,变量$ token为空。请帮助我评估我的代码。
谢谢
是的。但是,我如何在我的PHP文件中使用它。我得到这样的cURL字符串响应:$ result = curl_execute($ ch);我如何在$ result周围编写单引号。看看我上面的PHP文件实现。 @DeveloperH –
将您的代码更改为这个 '$ arr = json_decode($ result,true); $ token = $ arr ['access_token'];' –
好吧。我想我找到了问题所在。从Paypal返回的结果字符串包含其他字符,将字符串作为Json格式取消。我需要找到一种方法来摆脱字符串中的这些字符以将其转换为Json格式。 –