Skip to content

PHP curl request return with error #11197

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

Closed
kiran4455 opened this issue May 6, 2023 · 4 comments
Closed

PHP curl request return with error #11197

kiran4455 opened this issue May 6, 2023 · 4 comments

Comments

@kiran4455
Copy link

Description

The following code:

<?php
$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => FALSE,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 1,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($requestArr),
    CURLOPT_HTTPHEADER => [
        "accept: application/json",
        "cache-control: no-cache",
        "content-type: application/json"
    ],
]);

try {
    $response = curl_exec($curl);
    $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    $err = curl_error($curl);
    curl_close($curl);
    if ($err) {
        print_r($err);
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

Resulted in this output:

Operation timed out after 1000 milliseconds with 0 bytes received

But I expected this output instead:

Older version return no error

PHP Version

8.2.4

Operating System

22.04

@nielsdos
Copy link
Member

nielsdos commented May 6, 2023

Does it consistently fail on a newer version, and consistently work on an older version? What is the version number of the older version you tried? What's the curl version on your system?
There's not necessarily a bug here, it could very well be that the server was just slow in responding.

@Girgias Girgias changed the title PHp curl request return with error PHP curl request return with error May 8, 2023
@kiran4455
Copy link
Author

I Run same code in older PHP version 8.2.1 but not shown any error like this.
Both server have same curl version : 7.81.0

Is there any other configuration?

@iluuu1994
Copy link
Member

gd origin/PHP-8.2.1 origin/PHP-8.2.5 -- ext/curl

Details

diff --git a/ext/curl/interface.c b/ext/curl/interface.c
index 66f19f0def..2dd4a7bf65 100644
--- a/ext/curl/interface.c
+++ b/ext/curl/interface.c
@@ -60,6 +60,13 @@
 #include "ext/standard/file.h"
 #include "ext/standard/url.h"
 #include "curl_private.h"
+
+#ifdef __GNUC__
+/* don't complain about deprecated CURLOPT_* we're exposing to PHP; we
+   need to keep using those to avoid breaking PHP API compatibiltiy */
+# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#endif
+
 #include "curl_arginfo.h"
 
 #ifdef PHP_CURL_NEED_OPENSSL_TSL /* {{{ */
@@ -805,6 +812,8 @@ static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
 				if (Z_TYPE(retval) == IS_STRING) {
 					length = MIN((int) (size * nmemb), Z_STRLEN(retval));
 					memcpy(data, Z_STRVAL(retval), length);
+				} else if (Z_TYPE(retval) == IS_LONG) {
+					length = Z_LVAL_P(&retval);
 				}
 				zval_ptr_dtor(&retval);
 			}
@@ -1372,7 +1381,7 @@ static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpo
 				postval = Z_STR_P(prop);
 
 				if (php_check_open_basedir(ZSTR_VAL(postval))) {
-					return 1;
+					return FAILURE;
 				}
 
 				prop = zend_read_property(curl_CURLFile_class, Z_OBJ_P(current), "mime", sizeof("mime")-1, 0, &rv);
diff --git a/ext/curl/tests/CONFLICTS b/ext/curl/tests/CONFLICTS
new file mode 100644
index 0000000000..13368f8290
--- /dev/null
+++ b/ext/curl/tests/CONFLICTS
@@ -0,0 +1 @@
+curl
diff --git a/ext/curl/tests/curl_pause_001.phpt b/ext/curl/tests/curl_pause_001.phpt
new file mode 100644
index 0000000000..1fce0dd4af
--- /dev/null
+++ b/ext/curl/tests/curl_pause_001.phpt
@@ -0,0 +1,47 @@
+--TEST--
+Test CURL_READFUNC_PAUSE and curl_pause()
+--EXTENSIONS--
+curl
+--FILE--
+<?php
+include 'server.inc';
+$host = curl_cli_server_start();
+
+class Input {
+	private static $RESPONSES = [
+		'Foo bar ',
+		CURL_READFUNC_PAUSE,
+		'baz qux',
+		null
+	];
+	private int $res = 0;
+	public function __invoke($ch, $hReadHandle, $iMaxOut)
+	{
+		return self::$RESPONSES[$this->res++];
+	}
+}
+
+$inputHandle = fopen(__FILE__, 'r');
+
+$ch = curl_init();
+curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=input");
+curl_setopt($ch, CURLOPT_UPLOAD,       1);
+curl_setopt($ch, CURLOPT_READFUNCTION, new Input);
+curl_setopt($ch, CURLOPT_INFILE,       $inputHandle);
+curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+
+$mh = curl_multi_init();
+curl_multi_add_handle($mh, $ch);
+do {
+	$status = curl_multi_exec($mh, $active);
+	curl_pause($ch, CURLPAUSE_CONT);
+	if ($active) {
+		usleep(100);
+		curl_multi_select($mh);
+	}
+} while ($active && $status == CURLM_OK);
+
+echo curl_multi_getcontent($ch);
+?>
+--EXPECT--
+string(15) "Foo bar baz qux"
diff --git a/ext/curl/tests/curl_version_basic_001.phpt b/ext/curl/tests/curl_version_basic_001.phpt
index 6bc361bade..52652822a3 100644
--- a/ext/curl/tests/curl_version_basic_001.phpt
+++ b/ext/curl/tests/curl_version_basic_001.phpt
@@ -22,6 +22,6 @@
 int(%i)
 string(%i) "%s"
 string(%i) "%s"
-string(%i) "%s"
-string(%i) "%s"
+string(%i) "%S"
+string(%i) "%S"
 bool(true)
diff --git a/ext/curl/tests/responder/get.inc b/ext/curl/tests/responder/get.inc
index 4ed9ae0282..c139c8c7d4 100644
--- a/ext/curl/tests/responder/get.inc
+++ b/ext/curl/tests/responder/get.inc
@@ -4,6 +4,9 @@
     case 'post':
       var_dump($_POST);
       break;
+    case 'input':
+      var_dump(file_get_contents('php://input'));
+      break;
     case 'getpost':
       var_dump($_GET);
       var_dump($_POST);

Doesn't look like there's anything relevant in that diff. What happens if you set CURLOPT_TIMEOUT to something higher?

@github-actions
Copy link

No feedback was provided. The issue is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so. Thank you.

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