douneiben2240 2018-08-12 18:08
浏览 73
已采纳

如何卷曲json postfields?

Here's my code:

$pp = curl_init();
curl_setopt($pp, CURLOPT_URL, "https://api.paypal.com/v1/payments/billing-plans");
curl_setopt($pp, CURLOPT_HEADER, false);
curl_setopt($pp, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($pp, CURLOPT_SSLVERSION , 6);
curl_setopt($pp, CURLOPT_POST, true);
curl_setopt($pp, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($pp, CURLOPT_POSTFIELDS, "page_size=3&status=ALL&page=1&total_required=yes");
curl_setopt($pp, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "Authorization: Bearer $token"
));

$result_pp = curl_exec($pp);
echo $result_pp;

Here's the error I'm getting:

{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"b742c35fe6e0e"}

This is the documentation link:

https://developer.paypal.com/docs/api/payments.billing-plans/v1/#billing-plans_list

Basically just trying to pull a list of all my PayPal subscriptions, but it looks like I'm not sending something in JSON correctly...

  • 写回答

1条回答 默认 最新

  • dongyan3237 2018-08-12 18:14
    关注

    Since you are sending a POST request, the API believes you are attempting to create a billing plan, and that being the case, your post data is expected to be in json form - which it is not.

    According to the docs, to list your plans, the request type should be GET. So you need to add your params in the url:

    curl_setopt($pp, CURLOPT_URL, "https://api.paypal.com/v1/payments/billing-plans?page_size=3&status=ALL&page=1&total_required=yes");
    

    And remove these two lines:

    curl_setopt($pp, CURLOPT_POST, true);
    curl_setopt($pp, CURLOPT_POSTFIELDS, "page_size=3&status=ALL&page=1&total_required=yes");
    

    It's important to understand that many RESTful APIs use the request types to differentiate between actions on the same endpoint. PATCH, POST, PUT, GET and DELETE requests may all have different meanings depending on the API.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题