curlfedex

FEDEX Trade Documents Upload API


I am implementing the the FedEx ETD API. The code which I am using is here. Its give me error. Will you check this what the issue is/are

$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);

its give this error:

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

Solution

  • Instead of cURL use Client()

     $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);