cURL error(28) in laravel when accessing internal php files
When accessing internal PHP files using cURL in Laravel, you might encounter the cURL error(28). This error occurs when cURL cannot establish a connection to the target URL within the specified time limit. In the context of Laravel, this issue can arise when trying to access PHP files that are not publicly accessible, such as those located within the storage
or bootstrap
directories.
To resolve this issue, you need to configure cURL to ignore SSL certificate verification when making requests to internal PHP files. Here are the steps to do that:
- Create a new file named
curl.php
in the.env
directory of your Laravel project. This file will contain the cURL configuration options.
touch .env/curl.php
- Open the newly created file in a text editor and add the following PHP code:
<?php
return [
'curl' => [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
],
];
- Load the
curl.php
file in your.env
file by adding the following line at the end of the file:
FILEENV=curl
- Create a new helper function in your
app/Helpers/HelperServiceProvider.php
file to access the cURL configuration options:
namespace App\Helpers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
public function register()
{
if ($this->app->environment('local', 'testing')) {
$this->app->singleton('app.curl', function () {
return new \GuzzleHttp\Client([
'base_uri' => env('APP_URL'),
'defaults' => [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3094.110 Safari/56.0.3',
],
'verify' => false,
'timeout' => 60.0,
'max_redirects' => 5,
'connect_timeout' => 10.0,
'curl' => [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_ENCODING => '',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3094.110 Safari/56.0.3',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
],
],
]);
});
}
}
}
- Register the helper service provider in your
composer.json
file:
{
"autoload": {
"files": [
"app/Helpers/HelperServiceProvider.php"
]
},
"providers": [
"App\Helpers\HelperServiceProvider"
]
}
- Run the following command to install the Guzzle HTTP client:
composer require guzzlehttp/guzzle
- Now you can use the
app
facade to make cURL requests with the configured options:
use App\Facades\App;
$response = App::curl()->get('http://localhost/internal-file.php');
if ($response->getStatusCode() == 200) {
// Process the response
}
By following these steps, you should be able to make cURL requests to internal PHP files in Laravel without encountering the cURL error(28).