想通过codeigniter应用程序更新谷歌联系人

时间:2015-08-10 06:53:14

标签: php codeigniter google-oauth google-contacts

这是home_model.php(codeigniter应用程序)中的函数

public function edit_google_contact_oauth($oldname,$oldphone,$oldtype, $newname, $newphone, $newtype)
    {
        $checkname = $oldname. '(' . $oldtype. ')';
        $putname = $newname. '(' . $newtype . ')';
        session_start();//start session
        chdir(APPPATH.'libraries');
        require_once('Google/autoload.php');
        require('Google/Config.php');
        require('Google/Google_Client.php');
        chdir(FCPATH);

        $client_id = '371163949109-o19u9mlm4d6d9gi59v9inj9jjje6c46s.apps.googleusercontent.com';
        $client_secret = 'k9cE1eVSMN29pQzUO4ugWoUZ';
        $redirect_uri = 'http://neighborhood.apptechclient.com/restaurant/index.php/home/customer';

        $client = new Google_Client();
        $client -> setApplicationName('contact');
        $client -> setClientid($client_id);
        $client -> setClientSecret($client_secret);
        $client -> setScopes('https://www.google.com/m8/feeds');
        $client -> setRedirectUri($redirect_uri);
        $client -> setAccessType('online');

        if (isset($_GET['code'])) {
            $client->authenticate($_GET['code']);
            $_SESSION['token'] = $client->getAccessToken();
            header('Location: ' . $redirect_uri);
        }


        if(!isset($_SESSION['token']))
        {
            $url = $client->createAuthUrl();
        }else{
                $client->setAccessToken($_SESSION['token']);
                $token = json_decode($_SESSION['token']);
                $token->access_token;
                $curl = curl_init("https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=1000&access_token=" . $token->access_token);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_TIMEOUT, 10);
                $contacts_json = curl_exec($curl);
                curl_close($curl);
                $contacts = json_decode($contacts_json, true);
                $return = array();

                foreach($contacts['feed']['entry'] as $contact){
                    $return[] = array(
                    'id'=>$contact['id']['$t'],
                    'name' => $contact['title']['$t'],
                    'phone' => isset($contact['gd$phoneNumber'][0]['$t']) ? $contact['gd$phoneNumber'][0]['$t'] :false,
                    );
                }

                $count = -1;
                $contactId;
                foreach($return as $value) {
                $count++;

                if ((strcmp($value['name'], $checkname) == 0) && (strcmp($value['phone'], $oldphone) == 0)) {
                    $contactid=$value['id'];
                   }      
               }  



           }
    }

我想更新ID为contactId的联系人的联系方式,用$ putname替换该联系人的姓名,并用$ newphone替换联系人的phoneNumber。

我正在查看https://developers.google.com/google-apps/contacts/v3/#updating_contacts但我无法弄清楚我需要添加哪些PHP代码才能在谷歌联系人中进行更新。

其他一切都很好。我能够访问谷歌联系人和检索联系人等。我正在使用谷歌api php客户端。

看着https://developers.google.com/google-apps/contacts/v3/#updating_contacts,我感到很困惑。请告诉我我需要采取的步骤以及我需要添加哪些PHP代码才能成功更新Google通讯录中的联系人。谢谢。

0 个答案:

没有答案