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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)