【问题标题】:Batch Delete Domain Shared Contacts With PHP使用 PHP 批量删除域共享联系人
【发布时间】:2018-05-25 15:24:39
【问题描述】:

这是我使用的逻辑:

$xmlBuild = "<feed xmlns='http://www.w3.org/2005/Atom'
    xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
    xmlns:batch='http://schemas.google.com/gdata/batch'
    xmlns:gd='http://schemas.google.com/g/2005'
    xmlns:gContact='http://schemas.google.com/contact/2008'>";
$xmlBuild .= "<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'>";
$xmlBuild .= "<batch:id>1</batch:id><batch:operation type='delete'/>";
$xmlBuild .= "<id>http://www.google.com/m8/feeds/contacts/my.domain/base/1b93ef80b806243</id>";
$xmlBuild .= "<link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/my.domain/full/1b93ef80b806243/1812952240373020'/>";
$xmlBuild .= "</entry>";
$xmlBuild .= "</feed>";

$len = strlen($xmlBuild);
$options = array(
    "headers" => array(
        "Content-type" => "application/atom+xml; charset=UTF-8;",
        "Content-lenght" => $len
    ),
    "body" => $xmlBuild
);

$httpClient = $client->authorize();
$request = $httpClient->delete("https://www.google.com/m8/feeds/contacts/my.domain/full/batch", $options); 
$response = $request->getBody()->getContents();

print_r($response); //This prints "Contact ID not found."

// ??? why ???

我很确定我所做的一切都是正确的。对我来说,这似乎是一种错误的行为。我已经搜索了任何显示如何执行此操作的示例,但均无济于事。这里有没有人能够确定我的逻辑是否有问题?提前感谢您提供的任何帮助。

P.D.插入方法就像一个魅力。问题仅在于删除联系人。我还没有测试更新方法,但它都指出几乎与删除相同。不分批做删除是没有问题的。

【问题讨论】:

  • 如果您尝试其他批处理方法,例如添加或更新,它是否也不起作用?
  • @noogui 当我进行批处理插入时,添加工作正常。删除是它不起作用的那个。我已经发送了带有和不带有&lt;link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/my.domain/full/1b93ef80b806243/1812952240373020'/&gt; 的请求,但它仍然无法正常工作。不幸的是,文档中没有关于如何批量删除的示例。

标签: php google-api google-api-php-client google-admin-sdk


【解决方案1】:

似乎错误是因为您发送了 DELETE http 动词而不是 POST。此外,条目中的 XML 命名空间属性也不是必需的。

顺便说一句,这可能是一个错字,你写的是 'content-lenght' 而不是 content-length

不要将条目中的任何内容发送给删除,因为它不是必需的(也没有记录在案)。

$xmlBuild = "<feed xmlns='http://www.w3.org/2005/Atom'
    xmlns:batch='http://schemas.google.com/gdata/batch'
    xmlns:gd='http://schemas.google.com/g/2005'
    xmlns:gContact='http://schemas.google.com/contact/2008'>";
$xmlBuild .= "<entry>";
$xmlBuild .= "<batch:id>1</batch:id><batch:operation type='delete'/>";
$xmlBuild .= "<id>http://www.google.com/m8/feeds/contacts/my.domain/base/1b93ef80b806243</id>";
$xmlBuild .= "</entry>";
$xmlBuild .= "</feed>";

$len = strlen($xmlBuild);
$options = array(
    "headers" => array(
        "Content-type" => "application/atom+xml; charset=UTF-8;",
        "Content-length" => $len
    ),
    "body" => $xmlBuild
);

$httpClient = $client->authorize();
$request = $httpClient->post("https://www.google.com/m8/feeds/contacts/my.domain/full/batch", $options); 
// or $request = $httpClient->sendRequest('POST', "https://www.google.com/m8/feeds/contacts/my.domain/full/batch", $options); 

$response = $request->getBody()->getContents();

print_r($response);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多