首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建联系人时出现Google Contacts API奇怪错误

创建联系人时出现Google Contacts API奇怪错误
EN

Stack Overflow用户
提问于 2014-03-04 11:49:33
回答 1查看 91关注 0票数 0

当我试图在https://www.google.com/m8/feeds/contacts/default/full上发布一个新的联系人时,有人能解释一下错误的POST method does not support concurrency吗?我只是想创建一个新的联系人。

我不知道为什么我收到这个错误,谷歌搜索它和谷歌联系API文档无济于事。

还有没有人收到这个错误?

EN

回答 1

Stack Overflow用户

发布于 2015-02-26 20:28:39

试试这个,它很管用。:)

代码语言:javascript
复制
  include('application/libraries/google-contact-api/src/Google_Client.php') ; 
            $client= new Google_Client();

        $client->setApplicationName('application');
        $client->setClientId('xxxxxxxxxxxxxxxxxxxxxxxxxx');
        $client->setClientSecret('xxxxxxxxxxxxxxxxxxxxxx');
        $client->setScopes('https://www.google.com/m8/feeds/');
        $client->setRedirectUri('https://localhost/yourprjectname/controller/function_name');
        $client->setAccessType('online');
     $client->setApprovalPrompt('auto');  // to avoid each time the prompt for Accepting to sync the data

      if(isset($_GET['code']))
        {
            $client->authenticate();
            $_SESSION['token']=$client->getAccessToken();
           $client->setAccessToken($_SESSION['token']);
            $token=json_decode($_SESSION['token']);
            $token->access_token;
        }
        if(!isset($_SESSION['token']))
        {
            $url=$client->createAuthUrl();
            ?>
            <a href="<?= $url; ?>">Sync Contact</a>
        <?php
        }else
        {

    $doc = new DOMDocument();
    $doc->formatOutput = true;

    $entry = $doc->createElement('atom:entry');
    $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:atom', 'http://www.w3.org/2005/Atom');
    $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gd', 'http://schemas.google.com/g/2005');
    $doc->appendChild($entry);

    $cat = $doc->createElement('atom:category');
    $cat->setAttribute('scheme', 'http://schemas.google.com/g/2005#kind');
    $cat->setAttribute('term', 'http://schemas.google.com/contact/2008#contact');
    $entry->appendChild($cat);

    // add name element
    $name = $doc->createElement('gd:name');
    $entry->appendChild($name);
    $givenName = $doc->createElement('gd:givenName', 'HaPPY');
    $familyName = $doc->createElement('gd:familyName', 'srinivasan');
    $fullName = $doc->createElement('gd:fullName', 'venkat srinivasan');
    $name->appendChild($givenName);
    $name->appendChild($familyName);
    $name->appendChild($fullName);
    // add notes
    $content = $doc->createElement('atom:content', 'Notes');
    $content->setAttribute('type', 'text');
    $entry->appendChild($content);
    // add email element
    $email = $doc->createElement('gd:email');
    $entry->appendChild($email);
    $email->setAttribute('address', 'masterofdisaster.ps@gmail.com');
    $email->setAttribute('displayName', 'Panku');
    $email->setAttribute('primary', 'true');
    $email->setAttribute('rel', 'http://schemas.google.com/g/2005#work');

    // add im element
    $im = $doc->createElement('gd:im');
    $entry->appendChild($im);
    $im->setAttribute('address', 'kr.pankaj0086@gmail.com');
    $im->setAttribute('protocol', 'http://schemas.google.com/g/2005#GOOGLE_TALK');
    $im->setAttribute('primary', 'true');
    $im->setAttribute('rel', 'http://schemas.google.com/g/2005#home');
    // add phone element
    $ph = $doc->createElement('gd:phoneNumber', 'xxxxxxxxxx');
    $entry->appendChild($ph);
    $ph->setAttribute('rel', 'http://schemas.google.com/g/2005#home');

    //insert Address
    $address = $doc->createElement('gd:structuredPostalAddress');
    $address->setAttribute('primary', 'true');
    $address->setAttribute('rel', 'http://schemas.google.com/g/2005#work');
    $entry->appendChild($address);
    $postal = $doc->createElement('gd:postcode', '560078');
    $country = $doc->createElement('gd:country', 'India');
    $fulladd = $doc->createElement('gd:formattedAddress', 'BodhGaya');
    $address->appendChild($postal);
    $address->appendChild($country);
    $address->appendChild($fulladd);
     $contact_detail=$doc->saveXML();

            $url = 'https://www.google.com/m8/feeds/contacts/default/full?v=3&oauth_token=' .$token->access_token;
            //echo $url; exit;
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($curl, CURLOPT_POSTFIELDS, $contact_detail);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

            $curlheader[0] = "Content-Type: application/atom+xml";
            $curlheader[2] = "Content-length:" . strlen($contact_detail);
            $curlheader[1] = "Content-Transfer-Encoding: binary";
            curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);

            $xmlresponse = curl_exec($curl);
            echo curl_getinfo($curl, CURLINFO_HTTP_CODE);
            print_r($xmlresponse);
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22162506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档