【问题标题】:file_get_content with no returnfile_get_content 没有回报
【发布时间】:2018-12-21 10:53:10
【问题描述】:

我正在尝试在我的网站中实现 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?手动插入变量,它似乎工作正常。

我是不是忘记了什么?

谢谢

【问题讨论】:

标签: php recaptcha


【解决方案1】:

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

在这里查看答案How to post data in PHP using file_get_contents?

【讨论】:

  • 感谢您的回答。我尝试使用 GET 手动输入 URL,它似乎返回 ok: { "success": true, "challenge_ts": "2018-12-21T10:41:36Z", "hostname": "www.xxxxxxx.com " }
  • 您是否尝试使用 $_SERVER['HTTP_CLIENT_IP'] 而不是 $_SERVER['REMOTE_ADDR']
【解决方案2】:

我的包装器被禁用,这是我无法访问 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);

【讨论】:

    猜你喜欢
    • 2011-02-22
    • 2015-10-19
    • 2013-06-27
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 2021-03-26
    • 2016-05-26
    • 1970-01-01
    相关资源
    最近更新 更多