duanjiashai9140 2016-04-11 09:48 采纳率: 0%
浏览 49
已采纳

多个上传Google通讯录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!

  • 写回答

1条回答 默认 最新

  • dongwen7730 2016-04-11 10:42
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line