curl FEDEX贸易文件上传API

noj0wjuj  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(88)

我正在执行联邦快递ETD API。我使用的代码在这里。它的给予我错误。你会检查这个问题是什么/是

$url = "https://documentapitest.prod.fedex.com/sandbox/documents/v1/etds/upload";

$access_token = $authToken;

$file = fopen("fg_SO-000022647-WAR-2.pdf", "rb");
$textData = fread($file, filesize("g_SO-000022647-WAR-2.pdf"));
fclose($file);

$headers = array(
  "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
  "x-customer-transaction-id: 12XXXXXX",
  "Authorization: Bearer ".$access_token
);

$data = array(
"document" => '{
 "workflowName":"ETDPreshipment","name":"g_SO-000022647-WAR-2.pdf","contentType":"text/plain",
"meta":{"shipDocumentType":"COMMERCIAL_INVOICE","originCountryCode":"DK","destinationCountryCode":"BE"}}'
);

$files = array(
"attachment" => array("g_SO-000022647-WAR-2.pdf", $textData, "text/plain")
);

$postData = array(
'data' => json_encode($data),
'attachment' => new CURLFile('C:\xampp\htdocs\test\g_SO-000022647-WAR-2.pdf', 'text/plain', 'application/pdf')
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

$response = curl_exec($ch);

if ($response === false) {
    echo 'Curl error: ' . curl_error($ch);

    print_r(curl_error($ch));
}

curl_close($ch);

print_r($response);

字符串
它给予这个错误:

error
: 
"Bad Request"
message
: 
"Required request part 'document' is not present"
path
: 
"/document/v1/etds/upload"

6uxekuva

6uxekuva1#

使用Client()代替cURL

$accessToken = $this->accessToken;

    $fileInfo = pathinfo($invoiceLink);
    $fileName = $fileInfo['basename'];

    $client = new Client();
    
    $headers = [
        'Authorization' => 'Bearer '.$accessToken
    ];
    $options = [
        'multipart' => [
            [
            'name' => 'document',
            'contents' => '{
                "workflowName":"ETDPreshipment",
                "name":"'.$fileName.'",
                "contentType":"application/pdf",
                "meta":{
                    "shipDocumentType":"COMMERCIAL_INVOICE",
                    "originCountryCode":"US",
                    "destinationCountryCode":"US"}}'
            ],
            [
            'name' => 'attachment',
            'contents' => fopen($invoiceLink, 'r'),
            'filename' => $fileName,
            'headers'  => [
                'Content-Type' => '<Content-type header>'
            ]
        ]
    ]];

    $request = new Request('POST', 'https://documentapitest.prod.fedex.com/sandbox/documents/v1/etds/upload', $headers);
    $res = $client->sendAsync($request, $options)->wait();
    $response = json_decode($res->getBody(),true);

字符串

相关问题