phpamazon-web-servicesconnection-timeoutphp-curloutbound

PHP-cURL outbound call works in localhost but not in AWS hosted website despite allowing all traffic


I am using curl in my wordpress website(hosted on aws linux) to get some info from a few api's. The problem: I get connection timeout from one of the api only. I contacted the api provider as well and they said the api is working fine. The same code was working fine 5 days back.

Now after going through stack, and running some variations, the results are: 1) The same code works in localhost. 2) It also works in postman. 3) The code was copied from Postman to website but still getting timeout. 4) I edited the aws vpn nacl to allow all traffic in both inbound and outbound in addition to the ones recommended by aws, still getting timeout.

This is the basic function I am using, copied from postman and then modified:

function my2get_curl($myurl){
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => $myurl,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_USERAGENT =>$user_agent,
        CURLOPT_HTTPHEADER => array(

                "cache-control: no-cache",
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);
    $info = curl_getinfo($curl);
    curl_close($curl);

    if ($err) {
      echo "  cURL Error #:" . $err;
      echo json_encode($info,JSON_PRETTY_PRINT);

      die('');
    } else {
      return $response;
    }
}

Here is the result/curl error (same for http except the port number changes to 80):

    Failed to connect to www.travelpayouts.com port 443: Connection timed out

curl_info:

    { "url": "https:\/\/www.travelpayouts.com\/whereami?locale=en&callback=useriata&ip=<my.ip.address>", "content_type": null, "http_code": 0, "header_size": 0, "request_size": 0, "filetime": -1, "ssl_verify_result": 0, "redirect_count": 0, "total_time": 21.03931, "namelookup_time": 0.066799, "connect_time": 0, "pretransfer_time": 0, "size_upload": 0, "size_download": 0, "speed_download": 0, "speed_upload": 0, "download_content_length": -1, "upload_content_length": -1, "starttransfer_time": 0, "redirect_time": 0, "redirect_url": "", "primary_ip": "", "certinfo": [], "primary_port": 0, "local_ip": "", "local_port": 0 }

Is there something wrong with the code or (more likely) aws settings ? How exactly do i find out whats causing the problem and fix it.


Solution

  • You get this problem because Amazon IPs are closed to access our API. You can send your static IPs in Amazon and we will add them.