GuzzleHttp\异常\连接异常:cURL错误7:连接失败- Laravel

tct7dpnv  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(460)

我试图发布一个第三方API与我的控制器的原始机构,它的工作正常,当我从localhost测试它,但当我发布我的项目在服务器上(Cpanel),我得到这个错误:
GuzzleHttp\异常\连接异常:cURL错误7:无法连接。
下面是我在控制器中编写的代码示例:

use Illuminate\Support\Facades\Http;

    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;
      }
    }

我也尝试过使用GuzzleHttp和它在localhost上工作的相同的东西,当我在服务器上发布项目时不起作用。

use GuzzleHttp\Client;

    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;
      }
    }
kqlmhetl

kqlmhetl1#

然后禁用防火墙并再次测试。
可能有防火墙阻止您的请求

相关问题