file_get_contents showing null string for google captcha API

时间:2017-08-04 12:48:14

标签: php codeigniter recaptcha

When I hit my following url on chrome location bar, it is showing result in JSON.

This is the URL

But when I try to parse this using file_get_contents($url); It doesn't print anything. I tried file_get_Contents for different url, it is working properly for my other url.

Please suggest where am I getting wrong.

Thank You

2 个答案:

答案 0 :(得分:2)

As per given in document, its a POST request.

Here the cURL solution for recaptcha

$captcha = $this->input->post('g-recaptcha-response');
    $data = array(
        'secret' => RECAPTCHA_SECRET_KEY,
        'response' => $captcha
    );

    $verify = curl_init();
    curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
    curl_setopt($verify, CURLOPT_POST, true);
    curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
    $jsonresponse = curl_exec($verify);

Hope this help you.

答案 1 :(得分:0)

我建议你看一下使用CURL而不是file_get_content()。但是这里你需要它真的很糟糕

<?php
// <= PHP 5
$file = file_get_contents(<INSERT_URL>, true);
// > PHP 5
$file = file_get_contents(<INSERT_URL>, FILE_USE_INCLUDE_PATH);
?>