How to call Gmail API using PHP and cURL?

℡╲_俬逩灬. 提交于 2019-12-08 05:45:59

问题


I have the oauth access token but my get request fails. Below is my code:

    $response = array();
    $crl = curl_init();
    $newurl = "https://www.googleapis.com/gmail/v1/users/myemail@gmail.com/messages?access_token=" . $result['access_token'];
    echo $newurl . "</br>";
    curl_setopt($crl, CURLOPT_HTTPGET, true);
    curl_setopt($crl, CURLOPT_URL, $newurl);
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
    $reply = curl_exec($crl);
    if ( $reply ) {
        echo 'successfully retrieved messages</br>';
        echo $reply;
    } else {
        echo 'failed to get messages</br>'; // prints
        echo $reply; // prints nothing
    }

I confirmed that my key is correct: Does my Google oAuth2 Token look right?

myemail@gmail.com is certainly the one that provided access. Since this is PHP, I cannot debug because I do not have a console (I tried to print out the reply it prints nothing).


回答1:


It seems that you do not have the certificates for ssl installed properly.

I do not recommend disabling ssl check, but you can use:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);.

The thing you should do is:

curl_setopt($ch, CURLOPT_CAINFO, "path/to/cacert.pem");

Download (direct) the cert and use the above code.



来源:https://stackoverflow.com/questions/25952836/how-to-call-gmail-api-using-php-and-curl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!