返回通过api在google联系人中更新的联系人数 [英] return number of contacts updated in google contacts through api

查看:104
本文介绍了返回通过api在google联系人中更新的联系人数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 cakephp 中输入了 Google通讯录中的联系人 api
通过 Google xml 请求在Google中添加联系人,但我们如何获取总数
的联系人添加或更新在代码成功执行后在Google通讯录中



这里是代码:

  public function addContact($ token = null,$ atContacts = null){
foreach($ atContacts as $ contact){
$ xml =<<'EOF'
& atom:entry xmlns:atom ='http://www.w3.org/2005/Atom'
xmlns:gd ='http://schemas.google.com/g/2005'>
< atom:category scheme ='http://schemas.google.com/g/2005#kind'
term ='http://schemas.google.com/contact/2008#contact '/>
< title type =text> TITLE< / title>
< gd:name>
< gd:givenName>第一< / gd:givenName>
< gd:additionalName> ADDITIONALNAME< / gd:additionalName>
< gd:familyName>最后< / gd:familyName>
< gd:namePrefix> NAMEPREFIX< / gd:namePrefix>
< gd:nameSuffix> NAMESUFFIX< / gd:nameSuffix>
< / gd:name>
< gd:structuredPostalAddress rel ='http://schemas.google.com/g/2005#work'primary ='true'>
< gd:city> CITY< / gd:city>
< gd:street> STREET< / gd:street>
< gd:region> REGION< / gd:region>
< gd:postcode> POSTCODE< / gd:postcode>
< gd:country> COUNTRY< / gd:country>
< / gd:structuredPostalAddress>
< gd:phoneNumber rel ='http://schemas.google.com/g/2005#home'primary ='true'>
HOMEPHONENUMBER
< / gd:phoneNumber> \
< gd:phoneNumber rel ='http://schemas.google.com/g/2005#mobile'> MOBILENO< / gd:phoneNumber>
< gd:phoneNumber rel ='http://schemas.google.com/g/2005#work'> WORKPHONENO< / gd:phoneNumber>
< gd:email label =homeaddress =EMAILADDRESSdisplayName =DISPLAYNAME/>
< / atom:entry>
EOF;


$ xml = str_replace(TITLE,$ contact-> Title,$ xml);
$ xml = str_replace(EMAILADDRESS,$ contact-> EMailAddress,$ xml);
$ xml = str_replace(DISPLAYNAME,$ contact-> FirstName,$ xml);
$ xml = str_replace(HOMEPHONENUMBER,$ contact-> AlternatePhone,
$ xml);
$ xml = str_replace(MOBILENO,$ contact-> MobilePhone,$ xml);
$ xml = str_replace(WORKPHONENO,$ contact-> Phone,$ xml);
$ xml = str_replace(CITY,$ contact-> City,$ xml);
$ xml = str_replace(STREET,$ contact-> AddressLine,$ xml);
$ xml = str_replace(POSTCODE,$ touch-> ZipCode,$ xml);
$ xml = str_replace(REGION,$ contact-> State,$ xml);
$ xml = str_replace(COUNTRY,$ contact-> Country,$ xml);
$ auth_header = array('Content-Type:application / atom + xml; charset = utf-8;',
'授权:GoogleLogin auth ='。trim($ token),
'Content-length:'。strlen($ xml));
$ url ='https://www.google.com/m8/feeds/contacts/default/full';
$ curl = curl_init();
curl_setopt($ curl,CURLOPT_URL,$ url);
curl_setopt($ curl,CURLOPT_POST,5);
curl_setopt($ curl,CURLOPT_POSTFIELDS,$ xml);
curl_setopt($ curl,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ curl,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ curl,CURLOPT_HTTPHEADER,$ auth_header);
$ xmlresponse = curl_exec($ curl);
}
return $ xmlresponse;
}


解决方案

看起来你做任何验证,以检查联系人是否已成功添加,并且没有捕获任何cURL错误,所以与您的代码,因为它目前只是做..

  $ number_of_contacts = count($ atContact); 

如果假设你的$ atContact数组的结构。 / p>

这将为您尝试添加到Google的联系人数量。如果你在某一点添加了一些验证,并且想要更准确,那么创建一个新的变量...

  public function addContact($ token = null,$ atContacts = null){
$ number_of_contacts = 0;
foreach($ atContacts as $ contact){
$ xml =<<<'EOF'
...
if(no_errors){
$ number_of_contacts ++;
}


We have written code in cakephp to enter contacts in Google contacts through api, Contacts have added in Google through Google xml request , but how can we get total number of contacts added or updated in Google contacts after code successfully executed

here is code:

public function addContact($token=null,$atContacts=null){
foreach ( $atContacts as $contact ) {
$xml = <<<'EOF'
    <atom:entry xmlns:atom='http://www.w3.org/2005/Atom'               
     xmlns:gd='http://schemas.google.com/g/2005'>
     <atom:category scheme='http://schemas.google.com/g/2005#kind'       
     term='http://schemas.google.com/contact/2008#contact'/>
       <title type="text">TITLE</title>
       <gd:name>
     <gd:givenName>First</gd:givenName>
     <gd:additionalName>ADDITIONALNAME</gd:additionalName>
  <gd:familyName>Last</gd:familyName>
 <gd:namePrefix>NAMEPREFIX</gd:namePrefix>
 <gd:nameSuffix>NAMESUFFIX</gd:nameSuffix>
  </gd:name>
 <gd:structuredPostalAddress rel='http://schemas.google.com/g/2005#work' primary='true'>
<gd:city>CITY</gd:city>
<gd:street>STREET</gd:street>
<gd:region>REGION</gd:region>
<gd:postcode>POSTCODE</gd:postcode>
<gd:country>COUNTRY</gd:country>
</gd:structuredPostalAddress>
  <gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='true'>
 HOMEPHONENUMBER
 </gd:phoneNumber>\
<gd:phoneNumber rel='http://schemas.google.com/g/2005#mobile'>MOBILENO</gd:phoneNumber>
<gd:phoneNumber rel='http://schemas.google.com/g/2005#work'>WORKPHONENO</gd:phoneNumber>
<gd:email label="home" address="EMAILADDRESS" displayName="DISPLAYNAME" />
</atom:entry>
EOF;


        $xml = str_replace ( "TITLE", $contact->Title, $xml );
        $xml = str_replace ( "EMAILADDRESS", $contact->EMailAddress, $xml );
        $xml = str_replace ( "DISPLAYNAME", $contact->FirstName, $xml );
        $xml = str_replace ( "HOMEPHONENUMBER", $contact->AlternatePhone, 
                    $xml );
        $xml = str_replace ( "MOBILENO", $contact->MobilePhone, $xml );
        $xml = str_replace ( "WORKPHONENO", $contact->Phone, $xml );
        $xml = str_replace ( "CITY", $contact->City, $xml );
        $xml = str_replace ( "STREET", $contact->AddressLine, $xml );
        $xml = str_replace ( "POSTCODE", $contact->ZipCode, $xml );
        $xml = str_replace ( "REGION", $contact->State, $xml );
        $xml = str_replace ( "COUNTRY", $contact->Country, $xml );
$auth_header = array ('Content-Type: application/atom+xml; charset=utf-8;',
'Authorization: GoogleLogin auth=' . trim ( $token ),
                'Content-length:' . strlen ( $xml ) );
        $url = 'https://www.google.com/m8/feeds/contacts/default/full';
        $curl = curl_init ();
        curl_setopt ( $curl, CURLOPT_URL, $url );
        curl_setopt ( $curl, CURLOPT_POST, 5 );
        curl_setopt ( $curl, CURLOPT_POSTFIELDS, $xml );
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, TRUE );
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $curl, CURLOPT_HTTPHEADER, $auth_header );
        $xmlresponse = curl_exec ( $curl );
                }
    return $xmlresponse;
}

解决方案

In your code it does not appear that you do any validation to check that the contact was added successfully and you don't catch any cURL errors either, so with your code as it currently stands just do ..

$number_of_contacts = count($atContact);

If taken a guess to the structure of your $atContact array.

That will give you number of contacts you are trying to add to Google. If you add some validation at some point and want to make that more accurate, then create a new variable...

public function addContact($token=null,$atContacts=null){
$number_of_contacts = 0;
foreach ( $atContacts as $contact ) {
   $xml = <<<'EOF'
   ...
   if(no_errors){
       $number_of_contacts++;
   }

这篇关于返回通过api在google联系人中更新的联系人数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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