search

Home  >  Q&A  >  body text

Laravel: GuzzleHttp\Exception\ConnectException: cURL Error 7: Connection failed

I'm trying to publish a 3rd party Api with raw principal using my controller, when I test it from localhost it works fine, but when I publish my project on the server (Cpanel) I get This error:

GuzzleHttpExceptionConnectException: cURL Error 7: Connection failed.

Here is a sample of my code inside the controller:

use IlluminateSupportFacadesHttp;

    public function testApi(){
      $array = [
        'FullName' => 'Full Name',
        'PhoneNumber' => '9999999999',
        'Date' => '2022-06-26 17:20',
        'Note' => '',
      ];
      try {
        $response = Http::withBody(json_encode($array) , 'application/json')
          ->post('https://example');
        return $response->status();
      } catch (Exception $exception){
        return $exception;
      }
    }

I also tried using GuzzleHttp and the same thing it works on localhost but doesn't work when I publish the project on the server.

use GuzzleHttpClient;

    public function testApi(){
      $array = [
        'FullName' => 'Full Name',
        'PhoneNumber' => '9999999999',
        'Date' => '2022-06-26 17:20',
        'Note' => '',
      ];
      try {
          $client = new Client();
          $response = $client->request('POST', 'https://example', [
             'body' => json_encode($array),
             'headers' => [
                 'Content-Type' => 'application/json',
             ]
         ]);
        return $response->getStatusCode();
      } catch (Exception $exception){
        return $exception;
      }
    }


P粉388945432P粉388945432370 days ago687

reply all(1)I'll reply

  • P粉939473759

    P粉9394737592023-11-10 12:35:16

    Before disabling the firewall and testing again.

    Firewalls may be blocking your request

    reply
    0
  • Cancelreply