phpcurlfedex

FEDEX Ship API returns compressed info on some errors


I have implemented Fedex API using Check Rates API and can create labels using Ship API. Both worked ok in sandbox so I got my label certified and went live. API is supposed to return JSON. Occasionally, in both live and sandbox, API calls using cURL return non-JSON responses. Never have an issue getting good json responses using OAuth or Check Rates API calls. However, Ship API can return good json but can also return compressed responses.

Here is my curl request.

function fedexRequest($endpoint, $post, $header = null) {
    //$ch = curl_init('https://apis-sandbox.fedex.com/' . $endpoint); // sandbox
    $ch = curl_init('https://apis.fedex.com/' . $endpoint);    // live
    curl_setopt_array($ch, [
        CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_CONNECTTIMEOUT => 5,    CURLOPT_POSTFIELDS => $post,
    ]);
    if ($header) curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    return curl_exec($ch);
}

I get both json and compressed responses using this code. $post is the json payload I send them. Spoiler Alert! The errors are usually details of something in the payload. First error was setting packagingType = FEDEX_PAK for something that weights over 20 pounds. Another was using CM instead of IN on dimensions units.

My calls to Fedex web services support are hit or miss diagnosing non-json error responses. The responses look like code in browser: Black diamonds with white ? and random spaces, letters and numbers.

First person told me exactly what they saw on their end while looking at my account api calls, told me the cause of the error and the solution, and could even see my good label response in real time after I implemented the setting they recommended. Wish I got his direct line for callbacks. Other calls to support, several today, did not get to a person who would do the same diagnosis help. Last one said, you are getting a compressed response so deal with it. They said their manager said it was my code that caused the compressed response.

This is confusing, while in sandbox I did see errors in good json responses and build ways to parse those for end users appropriately. Also saw these code responses. I thought it was just the known bugginess of their sandbox, but apparently some of their errors become compressed.

So, I have a couple questions.

Is the compression caused by my cURL request?

If I set CURLOPT_ENCODING in my request, will it handle these responses more appropriately?

If I get a compressed response, is gzread or ZipArchive the best ways to unzip it and get at the actual error code, assuming its in there?


Solution

  • Seems like cURL request to Fedex can return both compressed and uncompressed responses. Adding CURLOPT_ENCODING => "" to cURL setopt array automatically uncompressed the gzip response.