温馨提示:本文翻译自stackoverflow.com,查看原文请点击:web - PHP Curl fails on Ubuntu when using PHP 7.2 but works on Mac with PHP 7.3.9
curl encoding php web

web - 使用PHP 7.2时,Ubuntu上的PHP Curl失败,但在具有PHP 7.3.9的Mac上可用

发布于 2020-04-08 12:21:58

仅当getAnalysis()在我的Ubuntu计算机运行时,它仍可在Mac的多个版本(包括最新版本)运行时才会出现以下错误我尝试通过使用该curl_error功能对错误进行故障排除,但未打印任何内容。


  • 失败的Ubuntu的使用PHP 7.2.24-0ubuntu0.18.04.2卷曲 7.58.0

  • 他的工作是苹果PHP 7.3.9卷曲 7.64.1

错误:

<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>

码:

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;
}

查看更多

提问者
Joseph A Mendoza
被浏览
89
Joseph A Mendoza 2020-02-03 11:56

我终于找到了解决方案。我只需要改变

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

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

原因是我的要求过大,需要让curl知道会发生什么