cURL running slow in Wordpress

2 weeks ago 20

I person the pursuing codification successful 1 of my Wordpress leafage templates:

$url = "https://xxxxxxxxxxxxxxx/"; $headers = array( 'Accept: application/json', 'Content-Type: application/json', ); $args = [ "ctype" => sanitize_text_field($_GET['ctype']), ]; if ($args['ctype'] == "test") { $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, wp_json_encode($args)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = json_decode(curl_exec($ch), true); // Check HTTP presumption code if (!curl_errno($ch)) { $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); echo "HTTP Code: $http_code"; } other { echo 'cURL Error: ' . curl_error($ch) . "\n"; } curl_close($ch); }

My codification is executing highly slow. I initially had CURLOPT_TIMEOUT acceptable to 10. This was waiting for 10 seconds for the publication to execute. Then erstwhile I power it to 2, it was moving rather accelerated but I deliberation determination is inactive a bigger problem.

In the if (!curl_errno($ch)) statement, it ever goes to the other connection and echoes retired cURL Error: Operation timed retired aft 2001 milliseconds with 0 bytes received. I don't truly recognize wherefore it is ever going into the other statement.

I person tried different cURL directives specified as:

curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); curl_setopt($ch, CURLOPT_SSL_SESSIONID_CACHE, true); curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 3600); curl_setopt($ch, CURLOPT_TCP_FASTOPEN, true); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);

but nary of these seemed to person immoderate effect.


Detailed timings based connected curl_setopt($ch, CURLOPT_TIMEOUT, 10):

CURLINFO_TOTAL_TIME: 10s CURLINFO_NAMELOOKUP_TIME: 0.001s CURLINFO_CONNECT_TIME: 0.156s CURLINFO_PRETRANSFER_TIME: 0.473s CURLINFO_STARTTRANSFER_TIME: 0s CURLINFO_HEADER_OUT: thing returned
Read Entire Article