繁体   English   中英

file_get_content没有回报

[英]file_get_content with no return

我正在尝试在我的网站中实现reCAPTCHA,除了从file_get_contents()返回的内容之外,其他所有内容似乎都正常运行。

这是我的代码:

if ($_REQUEST["send"] == 1){

    // access
    $secretKey = 'my_key';
    $captcha = $_POST['g-recaptcha-response'];
    $ip = $_SERVER['REMOTE_ADDR'];

    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
    $responseKeys = json_decode($response,true);

    echo ($responseKeys);exit;

    if(intval($responseKeys["success"]) !== 1) {
        $message = 'Invalid reCAPTCHA';
    } else {

        $msg = 'content';

        send_mail('send_to',"Subject",$msg);
        header("location:index.php?send=1");exit;

    }

} 

我的变量响应返回空。

我试图打开https://www.google.com/recaptcha/api/siteverify 手动插入变量,它似乎工作正常。

我忘记了什么吗?

谢谢

他们的API等待POST请求。 您的代码发送GET请求。

在此处查看答案如何使用file_get_contents在PHP中发布数据?

我的包装器被禁用,这是为什么我无法到达URL并获得退货的原因。

由于我无权访问php.ini,因此解决方法是使用curl发送请求,以下是代码:

    $url = "https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip;
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        echo curl_error($ch);
        echo "\n<br />";
        $response = '';
    } else {
        curl_close($ch);
    }

    if (!is_string($response) || !strlen($response)) {
        echo "Failed to get contents.";
        $contents = '';
    }

    $responseKeys = json_decode($response,true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM