简体   繁体   English

使用PHP Codeigniter访问Google Contact API

[英]Access Google Contact API with PHP Codeigniter

Are there any posts/tutorials about accessing Google Contact API with CodeIgniter framework? 是否有关于使用CodeIgniter框架访问Google Contact API的帖子/教程?
I have googled it a lot found just one post which using Zend Libraries with Codeigniter. 我用google搜索了很多只发现一个使用Zend Libraries和Codeigniter的帖子。
Went through some PHP implementation of Contact API. 通过Contact API的一些PHP实现。 But all of them are using Curl to talk to API. 但他们都使用Curl与API交谈。
Can I use the same way as a Codeigniter model class? 我可以使用与Codeigniter模型类相同的方式吗? or should I consider accessing gmail contacts using javascript? 或者我应该考虑使用javascript访问Gmail联系人?

Please help. 请帮忙。

I used oauth library for codeigniter http://getsparks.org/packages/oauth2/versions/HEAD/show 我使用oauth库进行codeigniter http://getsparks.org/packages/oauth2/versions/HEAD/show

set scope in the spark->oauth->libries->provider->google like 在spark-> oauth-> libries-> provider-> google中设置范围

public function __construct(array $options = array()) {
    // Now make sure we have the default scope to get user data
    empty($options['scope']) and $options['scope'] = array(
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.google.com/m8/feeds',
    );

Add following code in the same file 在同一文件中添加以下代码

public function curl_file_get_contents($email, OAuth2_Token_Access $token) {
    $url = "https://www.google.com/m8/feeds/contacts/$email/full?max-results=" . 25 . "&oauth_token=" . $token->access_token;
    $curl = curl_init();
    $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
    curl_setopt($curl, CURLOPT_URL, $url); //The URL to fetch. This can also be set when initializing a session with curl_init().
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); //The number of seconds to wait while trying to connect. 

    curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request.
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header.
    curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
    curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute.
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //To stop cURL from verifying the peer's certificate.
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

    $contents = curl_exec($curl);
    curl_close($curl);

    return $contents;
}

then used this code for the google contant in controller 然后将此代码用于控制器中的google内容

public function google() {
        $this->load->helper('url_helper');
        $this->load->spark('oauth2');
        $provider = $this->oauth2->provider('google', array(
            'id' => GOOGLE_APP_ID,
            'secret' => GOOGLE_APP_SECRET,
                ));

        if (!$this->input->get('code')) {
            // By sending no options it'll come back here
            $provider->authorize();
        } else {
            // Howzit?
            try {
                $token = $provider->access($_GET['code']);

                $user = $provider->get_user_info($token);
                $email = $user['email'];

                $xmlresponse = $provider->curl_file_get_contents($email, $token);
                if ((strlen(stristr($xmlresponse, 'Authorization required')) > 0) && (strlen(stristr($xmlresponse, 'Error ')) > 0)) {
                    echo "<h2>OOPS !! Something went wrong. Please try reloading the page.</h2>";
                    exit();
                }
                $xml = new SimpleXMLElement($xmlresponse);
                $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
                $result = $xml->xpath('//gd:email');
                $this->data['email_address'] = $result;
print_r($result);exit;
            } catch (OAuth2_Exception $e) {
                show_error('That didnt work: ' . $e);
            }
        }

    }

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

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