0

I have the following code in one of my Wordpress page 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 status code
  if (!curl_errno($ch)) {
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    echo "HTTP Code: $http_code";
  } else {
    echo 'cURL Error: ' . curl_error($ch) . "\n";
  }
  curl_close($ch);
}

My code is executing extremely slow. I initially had CURLOPT_TIMEOUT set to 10. This was waiting for 10 seconds for the script to execute. Then when I switch it to 2, it was running quite fast but I think there is still a bigger problem.

In the if (!curl_errno($ch)) statement, it always goes to the else statement and echoes out cURL Error: Operation timed out after 2001 milliseconds with 0 bytes received. I don't really understand why it is always going into the else statement.

I have tried other cURL directives such 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 none of these seemed to have any effect.


Detailed timings based on 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: nothing returned
7
  • 10s is a long time but you've provided no information on what happens during those 10s, e.g. does it take 1s to connect and 9s are spent recieving the response? Is most of the time spent connected but waiting for bytes from the server? Is it mostly spent in DNS resolution? It looks like the only debugging information you're collecting is curl errors and HTTP error codes. Is there a reason you've bypassed WordPress and gone straight to curl to make your requests? Keep in mind you've asked this in a WordPress stack but your code bypasses WordPress entirely and uses generic curl PHP
    – Tom J Nowell
    Commented Jun 5 at 10:35
  • have you confirmed your server is capable of connecting to the server you're making a request to? A timeout connecting isn't slowness, in the same way that a slow moving road is not the same as a closed road
    – Tom J Nowell
    Commented Jun 5 at 10:37
  • @TomJNowell Yes I tried Wordress wp_remote_post() but it was giving a similar issue, so I resorted to PHP cURL. I've updated my post above to add some additional details about the timings. Commented Jun 5 at 11:57
  • 1
    the edits suggest it did connect but the remote server sent no headers or response. Note that if we're being strict your code is fast, it's just blocked waiting for the remote server and networking. Have you replicated this request locally to discover how long the request actually takes or added logging at the remote end to see what the remote server thinks the execution time? Making HTTP requests is one of the heaviest most expensive operations you can make, especially on the frontend and uncached. Do you control the other end or is it a 3rd party? It might not be fixable at this end
    – Tom J Nowell
    Commented Jun 5 at 12:30
  • 1
    I managed to resolve the issue. It was in fact caused by the remote server not sending back any response. Once the configuration was fixed on the remote server, the timeout issue went away and everything seems to be working as expected. Commented Jun 6 at 11:39

1 Answer 1

0

The timeout issue, in my case, was caused by the remote server not sending back any response. I had to fix configuration on the remote server to send back a response. That has resolved the issue.

Not the answer you're looking for? Browse other questions tagged or ask your own question.