Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CURL issues in 8.3.7 #14184

Closed
HeavenCore opened this issue May 9, 2024 · 3 comments
Closed

CURL issues in 8.3.7 #14184

HeavenCore opened this issue May 9, 2024 · 3 comments

Comments

@HeavenCore
Copy link

HeavenCore commented May 9, 2024

Description

The following code (unchanged for years) is suddenly breaking in PHP 8.3.7 64bit NTS on IIS 10

<?php         
        $ch  = curl_init();
        $httpHeaders = array("Cache-Control: no-cache");
        $postStr = "grant_type=authorization_code&code=REDACTED&redirect_uri=https%3A%2F%2Fwww.REDACTED.co.uk%2Ffoobar%2Fnhs-login%2F&client_id=REDACTED&client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer&client_assertion=REDACTED";
		
            if ($postStr != "") {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr);
            }

        curl_setopt($ch, CURLOPT_ENCODING, "");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeoutInSeconds);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");

        $response->response = curl_exec($ch);

All of a sudden curl_exec() is returning false, more interestingly, curl_error($ch) returns an empty string!

Rolling back to PHP 8.3.3 fixes the problem and everything works as normal.

Digging a little deaper I added extra logging:

<?php      
    private function getCurlError($curlHandle) {
        $ermsg = parseString(curl_error($curlHandle));
        $ercode = parseString(curl_errno($curlHandle));
        $ermsgcode = parseString(curl_strerror($ercode));

        return "ErrorNo: " . $ercode . ", ErrorNoStr: " . $ermsgcode . ", " . $ermsg;
    } 

        //#### Fetch Results
        $response->responseCode = parseInt(curl_getinfo($ch, CURLINFO_HTTP_CODE));

        //#### Check Results
        if (!$response->response) {
            $response->errorStr = "HTTP Error: CURL RETURNED FALSE (" . $this->getCurlError($ch) . ") with status code " . $response->responseCode . ": " . $url;
            $response->result = false;
        } elseif ($response->responseCode < 200 || $response->responseCode > 299) {
            $response->errorStr = "HTTP Error: INVALID HTTP STATUS (" . $response->responseCode . " - expected 2xx): " . $url . " #### RESPONSE: " . $response->response;
            $response->result = false;
        } elseif (parseString($response->response) === "" || parseString($response->response) === "ERROR") {
            $response->errorStr = "HTTP Error: BAD RESPONSE (" . $response->response . "): " . $url;
            $response->result = false;
        } else {
            $response->response = parseString($response->response);
            $response->result = true;
        }

This gave me:

HTTP Error: CURL RETURNED FALSE (ErrorNo: 23, ErrorNoStr: Failed writing received data to disk/application, ) with status code 200

Note the http 200 - it looks like the CURL request is successfull, but CURL is returning false because of some disk writing issue?

PHP Version

PHP 8.3.7

Operating System

Windows 11 / WIndows Server 2016+

@HeavenCore
Copy link
Author

HeavenCore commented May 9, 2024

I see a similar issue in composer github, they suggest removing CURLOPT_ENCODING, "" - PHP docoumentation says: "If an empty string, "", is set, a header containing all supported encoding types is sent." - that appears not to be the case in 8.3.7 - i'll test this now.

Edit 1: looks like CURL renamed it! https://curl.se/libcurl/c/CURLOPT_ACCEPT_ENCODING.html

Edit 2: OK, yes, removing curl_setopt($ch, CURLOPT_ENCODING, ""); does indeed fix the problem!

I think there are a few issues here:

  1. PHP documenation here needs updating
  2. curl_error() needs updating to give useful info instead of an empty string?
  3. Why is curl_strerror() complaining about writing to disk if it's a header issue???
  4. CURLOPT_ENCODING needs some TLC - either renaming to CURLOPT_ACCEPT_ENCODING or keeping as CURLOPT_ENCODING with some changes to ensure curl doesnt fallover if "" is passed as lots of people will be doing.

@shyim
Copy link

shyim commented May 9, 2024

This has nothing to do with PHP. It's an upstream curl problem.
curl/curl#13209

@Girgias
Copy link
Member

Girgias commented May 11, 2024

This is not a bug in PHP, thus closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants