Google reCaptcha v2 PHP - 仅适用于file_get_contents()

时间:2018-03-03 12:05:50

标签: php post recaptcha

我是PHP新手,尝试使用PHP和AJAX使用CurlPost()SocketPost()方法实现Google reCaptcha v2。目前一切都运行良好file_get_contents(),但我的提供商将很快锁定它,所以我需要重构我的代码。不知何故,我遇到了实现CurlPost()SocketPost()的问题 - 每当我尝试使用其中任何一个时,nginx都会回复403,说明Not a POST request(它在{{1}中的一个中被硬编码条款)。

我对else究竟有什么看法?下面是两段代码,一段与POST完美配合,第二段是投掷403的问题。

工作一个

file_get_contents()

没有工作,给403:

<?php

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    require("PHPMailer.php");
    require("Exception.php");
    require("SMTP.php");
    require('recaptcha-master/src/autoload.php');
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
            // Get the form fields and remove whitespace.
            $name = strip_tags(trim($_POST["name"]));
                    $name = str_replace(array("\r","\n"),array(" "," "),$name);
            $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
            $tel = trim($_POST["tel"]);
            $message = trim($_POST["message"]);
        $recaptchaSecret = "6LcjwkUUAAAAAENXcZtla40jNuPJGblZkYxLkXvf";
        $captcha = '';
        if(isset($_POST["captcha"]) && !empty($_POST["captcha"])){
        $captcha=$_POST["captcha"];
        }
        if(!$captcha){
              echo 'Prove that you are human';
              exit;
            }
                $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LcjwkUUAAAAAENXcZtla40jNuPJGblZkYxLkXvf&response=".$captcha."&remoteip=".$_SERVER["REMOTE_ADDR"]);
        $obj = json_decode($response);

         if($obj->success == true) {
            // Check that data was sent to the mailer.
            if ( empty($name) OR empty($message) OR empty($tel) OR empty($captcha) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
                // Set a 400 (bad request) response code and exit.
                http_response_code(400);
                echo "Oops! There was a problem with your submission. Please complete the form and try again.";
                exit;
            }

            // Build the email content.
            $email_content = "Имя: $name\n";
            $email_content .= "Телефон: $tel\n";
            $email_content .= "Email: $email\n";
            $email_content .= "Сообщение: $message\n";

            // Send the email.

        $mail = new PHPMailer();
        //Server settings
            $mail->SMTPDebug = 0;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'domain.tld';               // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'info';                     // SMTP username
            $mail->Password = 'password';             // SMTP password
            $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                                    // TCP port to connect to

        //Recipients
            //$mail->setFrom($name, $email);
            $mail->setFrom('info@domain.tld', 'DOMAIN.TLD');
            $mail->addAddress('info@domain.tld', 'Information');  // Add a recipient
            $mail->addReplyTo('info@domain.tld', 'Information');

        //Content
            $mail->Subject = 'Новое сообщение с сайта DOMAIN.TLD' ;
            $mail->Body    = $email_content;

        $success = $mail->send();
            echo "success"; 
        }
        } else {
            // Not a POST request, set a 403 (forbidden) response code.
            http_response_code(403);
            echo "Something went wrong. Please try again";
       }
?>

非常感谢任何提示!提前谢谢。

1 个答案:

答案 0 :(得分:0)

最后,我找到了public class broadcastReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { MainActivity instance = new MainActivity(); instance.showToast(AnyText); } } 使用标准google recaptcha库的工作解决方案,只是普通的curl。代码需要大量清理,但它正在工作。这是:

curl