如何使用PHP和cURL调用Gmail API? [英] How to call Gmail API using PHP and cURL?

查看:119
本文介绍了如何使用PHP和cURL调用Gmail API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有oauth访问令牌,但我的获取请求失败。下面是我的代码:

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
    }

我确认我的键是正确的:
我的Google oAuth2令牌是否正确?

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

myemail@gmail.com当然是提供访问的。因为这是PHP,我不能调试,因为我没有一个控制台(我试图打印出来的答复它什么也没打印)。

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).

推荐答案

您似乎没有正确安装ssl的凭证。

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

我不建议您停用ssl检查,但可以使用:

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

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);.

你应该做的事情是:

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

下载直接)证书,并使用上面的代码。

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

这篇关于如何使用PHP和cURL调用Gmail API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆