1

Hi I have a form I've made that works on my local machine but doesn't run the get_file_contents on the live server as it returns NULL on the response?

Form:

 <form name="contactUS" action="<?php echo get_template_directory_uri(); ?>/mail/contact-us-mail-recaptcha.php" method="post">

<input name="name" type="text" placeholder="Name">
<input name="email" type="email" placeholder="Email">
<input name="message" type="textarea" placeholder="Please describe your project and the work to be done?">
<div class="g-recaptcha" data-sitekey="<site key>"></div>
<button type="submit" class="nsc-button">Submit</button>
</form>

Server Code:

<?php

$recaptcha_secret = '<secret key>';
$ip = $_SERVER['REMOTE_ADDR'];
$captchaResponse = $_POST['g-recaptcha-response'];

$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$captchaResponse); 

$result = json_decode($response, true);

if($result == true){

//do success
}else {

    var_dump('ip:' . $ip);
    var_dump('captchaResponse:' . $captchaResponse);
    var_dump('response:' . $response);
    var_dump('result:' . $result);
    exit;

}
2
  • Make sure you added your domain URL in google developer recaptcha configuration. – Pratik Patel Jan 3 '19 at 7:35
  • thank you yes the url is added I confirmed this other wise there is an error recaptcha box on the form – danbdex Jan 3 '19 at 7:36
0

Tip A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

So most probably allow_url_fopen is set to false on your server.

But you shouldn’t use that function - use wp_remote_get instead.

3
  • I've done a phpinfo() and it shows allow_url_fopen is on so does file_get_contents not work at all with wordpress? why is it working locally? It's the same code locally and on server I'm just editing and copying straight across – danbdex Jan 3 '19 at 7:51
  • file_get_contents is PHP function - WP can't change how PHP works. So it's not WP that changes its behavior. You have to check PHP config on both servers. BUT, as I've already stated, you should use wp_remote_get instead (using file_get_contents with URLs is rather bad practice and can cause security problems). – Krzysiek Dróżdż Jan 3 '19 at 7:54
  • I will update it to wp_remote_get once its working thanks. I'll check the config is the same on both servers but as I already said the allow_url_fopen is on on the live server so should be working. – danbdex Jan 3 '19 at 8:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.