PHP-使用多个FormData进行卷曲

时间:2018-09-10 16:59:23

标签: php curl paypal

我想将此curl(在shell中工作)转换为PHP。它看起来很特别,因为它具有多个-F选项,其中1个用于JSON内容,另外1个用于文件上传。

如果下面提供的代码有效,则为数据。

curl -X POST https://api.sandbox.paypal.com/v1/customer/disputes/<dispute_id>/provide-evidence \
-H "Content-Type: multipart/related; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" \
-H "Authorization: Bearer <my_token>" \
-F 'input={
  "evidence_type": "PROOF_OF_FULFILLMENT",
  "evidence_info": {
  "tracking_info": [
    {
    "carrier_name": "FEDEX",
    "tracking_number": "122533485"
    }
  ]
  },
  "notes": "Test"
};type=application/json' \
-F 'file1=img/myimg.png'

但是我的问题是它不起作用:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, https://api.sandbox.paypal.com/v1/customer/disputes/<DISPUTE_ID>/provide-evidence);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$postFields = array
(
    'input' => '{
                "evidence_type": "PROOF_OF_FULFILLMENT",
                "evidence_info": {
                    "tracking_info": [
                    {
                        "carrier_name": "FEDEX",
                        "tracking_number": "122533485"
                    }
                ]
                },
                "notes": "Test"
            };type=application/json',
    'file' => 'file1=img/myimg.png'
);

$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$headers = array();
$headers[] = "Content-Type: multipart/related; boundary=" . $delimiter;
$headers[] = "Authorization: Bearer ".$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

$arr = json_decode($result);

return($arr);

我得到这个错误作为回报:

Array
(
    [name] => VALIDATION_ERROR
    [message] => Invalid request - see details
    [debug_id] => 799d8c12dd477
    [details] => Array
        (
            [0] => Array
                (
                    [location] => body
                    [issue] => MISSING_OR_INVALID_REQUEST_BODY
                )

        )

    [links] => Array
        (
        )

)

过去有人遇到过这个问题吗? 谢谢

0 个答案:

没有答案