davy_yg's avatar

file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0

I get the following error after checking the recaptcha:

file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0

Error on this line: 'g-recaptcha-response.recaptcha'=>'Harap centang recaptcha'])->validate();

Any clue why?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error message:

file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0

means that your PHP configuration has allow_url_fopen set to 0 (off), which disables the use of file_get_contents() to fetch data from URLs (like https://...). Many recaptcha libraries use file_get_contents() to verify the response with Google's API.

How to fix:

Option 1: Enable allow_url_fopen (Recommended if you control the server)

  1. Open your php.ini file.
  2. Find the line:
    allow_url_fopen = Off
    
  3. Change it to:
    allow_url_fopen = On
    
  4. Restart your web server (e.g., Apache or Nginx).

Option 2: Use cURL Instead of file_get_contents()

If you cannot change the server configuration, use cURL to make HTTP requests. For example, to verify recaptcha:

$recaptcha = $_POST['g-recaptcha-response'];
$secret = 'YOUR_SECRET_KEY';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'secret' => $secret,
    'response' => $recaptcha,
    'remoteip' => $_SERVER['REMOTE_ADDR']
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);

if ($result['success']) {
    // Recaptcha verified
} else {
    // Verification failed
}

Summary:

  • The error is because allow_url_fopen is disabled.
  • Enable it in php.ini, or
  • Use cURL to make HTTP requests instead of file_get_contents().

Let me know if you need a Laravel-specific example!

Please or to participate in this conversation.