Search code examples
phpgoogle-apigoogle-api-php-clientgoogle-contacts-apigoogle-data-api

Multiple upload Google Contacts Api XML?


i'm developing an application in PHP, that needs to upload 100 contacts the first time, i have developed a basic app, to upload a contact, but it takes near to 1,5 seconds to process the request:

        $before = microtime(true);
        $req = new Google_Http_Request("https://google.com/m8/feeds/contacts/" . $user_email . "/full/");
        $req->setRequestMethod("POST");
        $req->setPostBody($contact_xml);
        $req->setRequestHeaders(array('content-length' => strlen($contact_xml), 'GData-Version' => '3.0', 'content-type' => 'application/atom+xml; charset=UTF-8; type=feed'));

        $submit = $this->_gclient->getAuth()->authenticatedRequest($req);
        $sub_response = $submit->getResponseBody();
        $parsed = simplexml_load_string($sub_response);
        $client_id = explode("base/", $parsed->id);

        //Profiling
        $after = microtime(true);

I have tried to contcatenate to my entry twice or the times i needed but it doesn't work:

$contact_xml.="
<atom:entry xmlns:atom='http://www.w3.org/2005/Atom'
    xmlns:gd='http://schemas.google.com/g/2005'
    xmlns:gContact='http://schemas.google.com/contact/2008'>
  <atom:category scheme='http://schemas.google.com/g/2005#kind'
    term='http://schemas.google.com/contact/2008#contact'/>
  ...
  <gContact:groupMembershipInfo deleted='false'
        href='http://www.google.com/m8/feeds/groups/".$user_email."/base/6'/>
</atom:entry>

And the only thing i get from google is:

 [1] => SimpleXMLElement Object
    (
        [error] => SimpleXMLElement Object
            (
                [domain] => GData
                [code] => parseError
                [internalReason] => Parse Error
            )

    )

Which is like ...

  1. Am i doing something wrong, does Google give any kind of asyncronous request?
  2. Is even possible to do a multiple upload of google contact entry?
  3. If i make a loop with the individual upload function it takes too long for an amount of 100 contacts, that's the reason of the question.

Than you!


Solution

  • Finally thanks to DalmTo, the batch functionality of the GData fits perfectly in this problem. Here is an example of the feed you need to create in order to use the batch functionality:

    <feed xmlns='http://www.w3.org/2005/Atom'
    xmlns:gContact='http://schemas.google.com/contact/2008'
    xmlns:gd='http://schemas.google.com/g/2005'
    xmlns:batch='http://schemas.google.com/gdata/batch'>
    <entry>
        <batch:id>create</batch:id>
        <batch:operation type='insert'/>
        <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2008#contact'/>
        <gd:name>
        <gd:fullName>Example example</gd:fullName>
        <gd:givenName>Example</gd:givenName>
        <gd:familyName>Example</gd:familyName>
        </gd:name>
        <gd:email rel='http://schemas.google.com/g/2005#home' address='liz@gmail.com' primary='true'/>
    </entry></feed>
    

    then you just need to replicate your entry, with the operation you need to loop. Just a recomendation in order to get contacts created in your contacts inbox, you need to add:

    <gContact:groupMembershipInfo deleted = 'false' href = 'http://www.google.com/m8/feeds/groups/" . $user_email . "/base/6' />
    

    The request to Google would be like:

    $req = new Google_Http_Request("https://www.google.com/m8/feeds/contacts/" . $user_email . "/full/batch/");

    I hope this helps somebody.