URL Request but no Response Data in PHP

This happened to me when I had to add google recaptcha to a PHP contact form. Information from a contact from sent via a email.

<?php

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

if (isset($_POST)) {

    //exit();
    //$regex = "/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i";



$to = "test@gmail.com";
    $subject = "Messsage from " . $_POST['name'];


    $message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>Dear Securmatic (Private) Limited  Team.. You have got an inquiry from a client.</p>
<table>
<tr>
<td><b> First Name</b></td>
<td>" . $_POST['firsttname'] . "</td>
</tr>
<tr>
<td><b> Last Name</b></td>
<td>" . $_POST['lastname'] . "</td>
</tr>
<tr>
<td><b>Email</b></td>
<td>" . $_POST['email'] . "</td>
</tr>
<tr>
<td><b>Telephone Number</b></td>
<td>" . $_POST['tele'] . "</td>
</tr>
<tr>
<td><b>Comapny Name</b></td>
<td>" . $_POST['companyname'] . "</td>
</tr>
<tr>
<td><b>City</b></td>
<td>" . $_POST['city'] . "</td>
</tr>
<tr>
<td><b>Solution Interested</b></td>
<td>" . $_POST['solution'] . "</td>
</tr>
<tr>
<td><b>Message</b></td>
<td><p>" . $_POST['massage'] . "</p></td>
</tr>
</table>
<br/>
<p>Thanks.</p>
<br/>
</body>
</html>
";

// Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
    $headers .= 'From: <' . $_POST['email'] . '>' . "\r\n";
    $headers .= 'Cc: salestest@gmail.com' . "\r\n";
	// $test=  mail($to, $subject, $message, $headers);




    $secret_key = "6LfYn63432UUAdfdAAAABfdgdfgee45T6Pv1WQ";
      if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){

         $response_key = $_POST['g-recaptcha-response'];

$url = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret_key."&response=".$response_key;
/**
HERE usual code to get response is
  //$contents = file_get_contents($url);
But the response could not be received!
So that following code snippet had to be in middle to get the response back.
*/

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
// This is what solved the issue (Accepting gzip encoding)
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
$responsee = curl_exec($ch);
curl_close($ch);

/*
Up to here
*/
$responsee =''.$responsee.'';



  $response = json_decode($responsee);

   echo 'val : '. $response->{'success'};
if($response->{'success'} ){

      mail($to, $subject, $message, $headers);
      ?>
    <script>
      alert("Thank you for your interest in US");
          window.location="contactus.php";
        </script>
    <?php
  }
  else{

    ?>
   <script>alert("Message Sending Failed! Please contact via Emails.");
      window.location="contactus.php";
</script>
<?php
  }

  ?>


<?php }
      else{

    ?>
   <script>alert("Please Complete Captcha!");
      window.location="contactus.php";
</script>
<?php
      }

 } ?>

Leave a comment

Your email address will not be published. Required fields are marked *