Warm tip: This article is reproduced from stackoverflow.com, please click
curl encoding php web

PHP Curl fails on Ubuntu when using PHP 7.2 but works on Mac with PHP 7.3.9

发布于 2020-04-08 09:32:11

I get the following error only when running getAnalysis() on my Ubuntu machine while it still works on several versions of Mac (including the newest version). I tried troubleshooting the error by using the curl_error function however nothing was printed out.


  • Failed on: Ubuntu with PHP 7.2.24-0ubuntu0.18.04.2 and curl 7.58.0.

  • Worked on: Mac with PHP 7.3.9 and curl 7.64.1.

Error:

<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>
An error occurred while processing your request.<p>
Reference&#32;&#35;179&#46;96cadf17&#46;1580524623&#46;c5f345a
</BODY></HTML>

Code:

function getAnalysis($text)
{
    $APIKey = "APIKey";
    $URL="https://www.APIURL.com/API/";
    $data = json_encode(array('text' => $text));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, "apikey:$APIKey");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $result=curl_exec($ch);

    print("======================================\n");
    print($result);
    print("======================================\n");

    curl_close ($ch);

    return $result;
}
Questioner
Joseph A Mendoza
Viewed
71
Joseph A Mendoza 2020-02-03 11:56

I found the solution finally. I just need to change

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

to

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Expect:')); 

The reason was my request was to large and needed to let curl know what to expect