phpfedex

How should I pass the apiRequest "attachment" parameter for a FedEx Image upload


*** Updated:

I'm using the FedEx REST API for image upload (/documents/v1/lhsimages/upload), and I'm getting back an error:

    [errors] => stdClass Object
        (
            [code] => 1001
            [message] => Invalid request: invalid input : Invalid document details
        )

I suspect the issue might be related to how I'm passing the data up - typically in a cURL request for the FedEx API you would do

         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->apiRequest);

where $this->apiRequest is a json encoded request body. This works for other API requests. But the Upload Image is a bit tricky because it contains field data plus a file:

document: {"document": {"referenceId": "1234", "name": "LH2.PNG",    "contentType": "image/png",    "meta": { "imageType": "SIGNATURE","imageIndex": "IMAGE_1"}},"rules": {"workflowName": "LetterheadSignature" }}
attachment: file.PNG

What I was passing was

        $body = (object)[
            'referenceId' => 'Signature',
            'name' => $filename,
            'contentType' => $filetype,
            'rules' => (object)[
               'workflowName' => 'LetterheadSignature',
            ],
            'meta' => (object)[
               'imageType' => 'SIGNATURE',
               'imageIndex' => 'IMAGE_1',
            ]
        ];

         $sent_data = [
            'document' => json_encode($body),
            'attachment' => $file,
         ];

         $this->apiRequest = $sent_data;

I tried building $file parameter two ways:

      $handle = fopen($full_filename, "r");
      $file_contents = fread($handle, filesize($full_filename));
      fclose($handle);
      $file = base64_encode($file_contents);

and also:

      $file = curl_file_create($full_filename, $filetype, $filename);

where

      $filename = 'image-of-signature.png';
      $filetype = 'image/png';
      $full_filename = 'path-to-file' . $filename;

Neither worked; both gave me the Invalid request: invalid input : Invalid document details 1001 error.

The FedEx documentation describes the attachment parameter as:

string <file>
Input the actual document/file to be uploaded.

I have also tried passing the actual path to the file, and of course that doesn't work either.


Solution

  • Contrary to what is shown in the FedEx API documentation, the document element and the rules element are at the same level. The correct structure for the body is

          $body = (object)[
             'document' => (object)[
                'referenceId' => 'Letterhead',
                'name' => $filename,
                'contentType' => $filetype,
                'meta' => (object)[
                   'imageType' => 'LETTERHEAD',
                   'imageIndex' => 'IMAGE_2',
                ],
             ],
             'rules' => (object)[
                'workflowName' => 'LetterheadSignature',
             ]
          ];