Curl Timeouts

Discussion in '[bd] API' started by DeltaHF, Aug 7, 2018.

Tags:
  1. DeltaHF

    DeltaHF New Member

    This probably is not an issue with [bd] API itself, but I'm having some trouble connecting to it with curl in PHP.

    I wrote a custom WP plugin for my site which connects to the API. Both WP and XF are installed on the same server. The site is behind Cloudflare and SSL is enforced. I'm running Nginx with PHP-FPM 7.2.8 on a Centminmod dedicated server.

    The API appears to be working, but curl is reporting a lot of errors connecting to it. They all look something like this:

    Code:
    curl timeout connecting to XF API: Operation timed out after 8000 milliseconds with 0 out of -1 bytes received
    
    ...or this...

    Code:
    curl timeout connecting to XF API: Failed connect to [URL='http://www.gtplanet.net:443/']www.mydomain.net:443[/URL]; Operation now in progress
    
    Here's what my PHP looks like in my WordPress plugin:

    Code:
    curl_setopt( $ch, CURLOPT_URL, $url);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 6);
    curl_setopt( $ch, CURLOPT_TIMEOUT, 8);
    
    $body = curl_exec( $ch );
    
    if (curl_errno( $ch )) {
      error_log("curl timeout connecting to XF API: " . curl_error( $ch ), 0);
      return NULL;
    }
    
    curl_close( $ch );
    
    My own research into the problem indicates this might be caused by a problem with curl connecting to a server over TLS 1.2, which I confirmed my server does support via this script. Still, is this something I should specify with curl_setopt()?

    I know there might be some PHP/curl experts here... anybody have some ideas? I'm stumped. :(
     
  2. xfrocks

    xfrocks XenForo rocks!
    Staff Member

    If you are running both on the same server, probably best to connect to localhost/127.0.0.1 instead of using the domain name. Or you can add an entry to /etc/hosts to avoid the round trip to CloudFlare?
     
  3. DeltaHF

    DeltaHF New Member

    I'd definitely like to do that, but I don't think it's possible with my current configuration.

    I am using Cloudflare's Authenticated Origin Pulls, so technically only Cloudflare servers can access my site. From my understanding, Nginx will only talk to Cloudflare, which excludes localhost.

    Now that I'm looking into it a bit deeper, this seems to be an issue that others have encountered:

    https://community.cloudflare.com/t/authenticated-origin-pulls-and-localhost/16812
    https://community.cloudflare.com/t/authenticated-origin-pulls-cron-wget-curl-php-scripts/16356

    Assuming this really is what's causing the problem, I think my only option is to drop Cloudflare's Authenticated Origin Pulls and just block other IPs in the firewall. :(
     
  4. xfrocks

    xfrocks XenForo rocks!
    Staff Member

    Ah, I haven't used Authenticated Origin Pulls yet so I'm kinda clueless here. Sorry.
     
  5. alfa1

    alfa1 Active Member

    mhm. I seem to be encountering the same issue. @MattW
     
    1. xfrocks

      xfrocks XenForo rocks!
      Staff Member

      xfrocks @alfa1 You meant you have the same error message with the attachment uploads? Or you are trying to use [bd] API and encountered this exact issue?
       
    2. alfa1

      alfa1 Active Member

      alfa1 @xfrocks Sorry. I mean the same curl error message with attachment uploads. I totally missed the forum we are posting in.
       
      xfrocks likes this.
      1
Loading...