Php 返回通过api在google联系人中更新的联系人数

Php 返回通过api在google联系人中更新的联系人数,php,cakephp,google-api,google-contacts-api,Php,Cakephp,Google Api,Google Contacts Api,我们已经用cakephp编写代码,通过api在Google联系人中输入联系人, 谷歌通过谷歌xml请求增加了联系人,但我们如何才能得到总数 代码成功执行后在Google contacts中添加或更新的联系人数 以下是代码: public function addContact($token=null,$atContacts=null){ foreach ( $atContacts as $contact ) { $xml = <<<'EOF' <atom:entr

我们已经用cakephp编写代码,通过api在Google联系人中输入联系人, 谷歌通过谷歌xml请求增加了联系人,但我们如何才能得到总数 代码成功执行后在Google contacts中添加或更新的联系人数

以下是代码:

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

在您的代码中,您似乎没有执行任何验证来检查联系人是否已成功添加,也没有捕获任何cURL错误,因此,按照当前的代码,只需执行

$number_of_contacts = count($atContact);
如果猜测$atContact数组的结构

这将为你提供你试图添加到谷歌的联系人数量。如果您在某个点添加了一些验证,并希望使其更准确,那么请创建一个新变量

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

这取决于到目前为止您是如何编写代码的。我们能看看吗?是的。您可以评估代码我们不希望在google contacts中添加联系人,因为在autotask中,发送了42个联系人,但在google中只添加了39个联系人