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:
- Running the same API request using a direct cURL command in the terminal works perfectly.
- The same Laravel code runs without issues on my local development machine.
- Other network-dependent features in Laravel, like Slack notifications, also fail on the server.
What I’ve Tried:
-
Verified cURL Installation:
php -m | grep curlconfirms thatcURLis installed and enabled in PHP.
-
Checked cURL Version:
curl -VOutput:
curl 7.x.x -
PHP Configuration:
- Ensured
extension=curlis enabled in/etc/php.ini. - Restarted
php-fpmafter any configuration changes.
- Ensured
-
Verified OpenSSL:
- Confirmed that
opensslis installed and enabled in PHP.
- Confirmed that
-
Firewall/DNS Checks:
- No issues found; other outgoing connections (e.g.,
ping,curlfrom CLI) work fine.
- No issues found; other outgoing connections (e.g.,
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:
- Why would cURL work via the terminal but fail in Laravel?
- Are there any specific Laravel or PHP-FPM configurations that could cause this issue on CentOS?
- 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