0

I am integrating the third party API but getting port 443 after 127226 ms: Couldn't connect to server

GuzzleHttp\Exception\ConnectException cURL error 28: Failed to connect to xxxxx.xx port 443 after 127226 ms:
Couldn't connect to server (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://xxxxxxxxx/api/v1/service/recharge/recharge/getoperator

CODE

 public function mobileRecharge()
  {
    $data  = recharge::latest()->where('recharge_type', 1)->where('ret_id', Auth::id())->paginate(10);
    $circle_ref = [];
    $circle = [];
    $operators = [];
    
    try {
      $payload = [
        'partnerId' => ENV('PARTNERID'),
        'timestamp' => time(),
         
      ];
    return  $token = JWT::encode($payload, ENV('JWT_SECRET_KEY'), 'HS256');
      $client = new Client();
      $response = $client->post('https://API-URL/api/v1/service/recharge/recharge/getoperator', [
        'headers' => [
          'Token' => $token,
          'accept' => 'application/json',
        ],
      ]);
      $resp = json_decode($response->getBody(), true);
      return   $operators = $resp['data'];
      Log::debug($resp);

      //  RETURNING A VIEW WITH ALL MOBILE RECHARGE TRANSACTIONS HISTORY
      return view('employee.recharges.mobile.recharge', compact('data', 'circle', 'operators'));
    } catch (Exception $e) {
      return $e;
      return view('employee.recharges.mobile.recharge', compact('data', 'circle', 'operators'));
    }
  }

This is the full error message I get. I tried contacting the server provider they told me to white list the API IP. I did, but not working. It works fine in the POSTMAN.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • 2
    Where is your code? How have you whitelisted API IP? – Justinas Jun 21 '23 at 09:37
  • If you whitelisted any IP and tested this in Postman, keep in mind that your webserver might use a different IP – Nico Haase Jun 21 '23 at 09:41
  • Can you connect to other 3rd party locations from your app. Try getting a well known URL like example.com and see if that works just to be sure the issue is with this particular API – apokryfos Jun 21 '23 at 09:45
  • @Justinas https://support.cpanel.net/hc/en-us/articles/8408010789399-How-to-whitelist-an-IP-address-in-CSF-Firewall i followed this link sir. – Swapnil Mane Jun 21 '23 at 10:15
  • @NicoHaase sir. I gave them my server ip. it worked fine locally when I push the code on the server. it started giving me that error. – Swapnil Mane Jun 21 '23 at 10:16
  • @Justinas I have updated the code. – Swapnil Mane Jun 21 '23 at 10:19
  • Usually, whitelisting should only affect **incoming** connections – Nico Haase Jun 21 '23 at 11:02
  • 1
    Did you try to contact the 3rd party API provider or did you contact your own hosting provider? It seems that the 3rd party API is blocking the hosting provider you are using – apokryfos Jun 21 '23 at 11:42
  • i contacted the api provider and the hosting provider they both told me there is no issue from their sides. you have to check yourr code. I have whitelisted my server ip address into the THIRD party api dashboard still getting the issue but working fine on the local env. sir. @apokryfos – Swapnil Mane Jun 22 '23 at 03:45

1 Answers1

0

This is a timeout. It means the following.

  1. Your php software is able to use DNS to look up the hostname of the host where your third-party API runs. You gave it a good hostname.
  2. When your php software starts your curl request, it sends a connection request to that host, but never gets a response. curl gives up after about 2 minutes, and throws the error you showed us.

So the question is, what happened to your connection request?

We can rule out the API server being offline, because you got it to work from postman, probably running on your desktop machine.

So, the connection request is hitting a security firewall somewhere. When security firewalls see disallowed requests, they simply discard them instead of passing them along.

The firewall probably protects your API provider from incoming requests from random cybercreeps. It inspects the request and extracts its source IP address. The source is the machine where your php code runs. This might be a server in a data center somewhere (you didn't tell us about that). If so, it doesn't have the same IP address as your desktop machine where you run postman.

If the firewall doesn't recognize the source IP address, it discards the request.

So you need to somehow tell your API provider to add your server's IP address to their inbound allowlist (whitelist). If you already did this for your postman machine, you already know how.

It's possible, but less likely, that the firewall blocks outbound requests from the data center where your php program runs. It that case you'll have to somehow tell your own hosting provider to add the IP address of your API provider to their outbound-request allowlist.

And it's possible that both firewalls exist.

Why is this so complex? Because cybercreeps.

O. Jones
  • 103,626
  • 17
  • 118
  • 172