简体   繁体   中英

IIS PHP 5.5 - HTTP Error 411. The request must be chunked or have a content length

I am using PHP in IIS 7. Earlier I was having PHP 5.3 it was working fine. Now I migrated to PHP 5.5 and I am getting "HTTP Error 411. The request must be chunked or have a content length." error on curl requests.

I was googling the solution but unable to get the any hint.

Below is the sample code which I am using:

    $request = "https://example.com?searchTxt=" . $keyword . "&count=" . $count;
    $data = "[1]";
    $data_encoded = utf8_encode($data);
    // Initialize the session
    $session = curl_init($request);
    // Set curl options
    curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_encoded)));
    curl_setopt($session, CURLOPT_POST, 1);
    curl_setopt($session, CURLOPT_POSTFIELDS, $data_encoded);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($session, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($session, CURLOPT_AUTOREFERER, 1); 
    curl_setopt($session, CURLOPT_FAILONERROR, FALSE);
    curl_setopt($session, CURLOPT_USERPWD, USERNAME . ':' . PASSWORD);
    curl_setopt($session, CURLOPT_VERBOSE, TRUE); // Display communication with server
    curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_NTLM ) ; 
    curl_setopt($session, CURLOPT_CAINFO, CERTIFICATE_PATH);
    curl_setopt($session, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    // Make the request
    $response = curl_exec($session);

You don't need to add explicitly the Content Length in request header. cURL will add (count) the length and add it if needed.

Since strlen() is not multi-byte friendly, strlen(utf8_encode($data_encoded)) is not working as you expected. For UTF-8 characters, you should use mb_strlen() instead. See documentation

Also, if your $data_encoded is not encoded in ISO-8859-1, the function is also not working as you expect; the function is literally a ISO-8859-1 to UTF-8 converter. See documentation

Last, according to your code, $data_encoded comes from nowhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM