Laravel cURL Error 7: Couldn’t Connect to Server on CentOS (Works on Local and CLI)

Tiêu đề:

Laravel cURL Error 7: Couldn’t Connect to Server on CentOS (Works on Local and CLI)


Nội dung:

I’m running a Laravel application on a CentOS server, and when making HTTP requests using Http::post or Http::get, I encounter the following error:

cURL error 7: Couldn't connect to server (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

Observations:

  1. Running the same API request using a direct cURL command in the terminal works perfectly.
  2. The same Laravel code runs without issues on my local development machine.
  3. Other network-dependent features in Laravel, like Slack notifications, also fail on the server.

What I’ve Tried:

  1. Verified cURL Installation:

    • php -m | grep curl confirms that cURL is installed and enabled in PHP.
  2. Checked cURL Version:

    curl -V
    

    Output:

    curl 7.x.x
    
  3. PHP Configuration:

    • Ensured extension=curl is enabled in /etc/php.ini.
    • Restarted php-fpm after any configuration changes.
  4. Verified OpenSSL:

    • Confirmed that openssl is installed and enabled in PHP.
  5. Firewall/DNS Checks:

    • No issues found; other outgoing connections (e.g., ping, curl from CLI) work fine.

Server Environment:

  • OS: CentOS 7
  • PHP: 8.x
  • Web Server: Nginx
  • Laravel: 11.x

Reproducible Laravel Code:

$response = Http::post('https://server.test/api/getStock', [
    'kioskToken' => 'RDZINRHC4JNG9VM5KQMI',
    'userToken' => 'K9QM4805P0O51A37NWWWVO9TN83SDEYOZ2AP',
]);

if ($response->failed()) {
    logger()->error('API request failed: ' . $response->body());
}

Questions:

  1. Why would cURL work via the terminal but fail in Laravel?
  2. Are there any specific Laravel or PHP-FPM configurations that could cause this issue on CentOS?
  3. Could there be a difference in the network environment between CLI and Laravel running via the web server?

Any help or insights would be greatly appreciated!

I try to deploy to another server, or install curl again