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

时间:2014-03-04 03:49:33

标签: google-api google-contacts

当我尝试将新联系人发布到POST method does not support concurrency时,任何人都可以对错误https://www.google.com/m8/feeds/contacts/default/full有所了解。我只想创建一个新的联系人。

我不知道为什么我收到此错误并使用Google搜索并且谷歌联系API文档没有帮助。

有没有其他人收到此错误?

1 个答案:

答案 0 :(得分:-1)

试试吧。 :)

  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);
    }