我使用Google API PHP客户端与Contacts API进行交互,我可以获取标题,电子邮件地址和电话号码以保存正常。但是,我无法获取要保存的联系人的地址。我已经将请求中发送的内容与他们的文档进行了比较:https://developers.google.com/google-apps/contacts/v3/#creating_contacts所有内容似乎都相同。
以下是调用API的代码:
$doc = new \DOMDocument();
$doc->formatOutput = true;
$entry = $doc->createElement('atom:entry');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:atom', 'http://www.w3.org/2005/Atom');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gd', 'http://schemas.google.com/g/2005');
$doc->appendChild($entry);
$category = $doc->createElement('atom:category');
$category->setAttribute('scheme', 'http://schemas.google.com/g/2005#kind');
$category->setAttribute('term', 'http://schemas.google.com/contact/2008#contact');
$entry->appendChild($category);
$title = $doc->createElement('title', $name);
$entry->appendChild($title);
$email = $doc->createElement('gd:email');
$email->setAttribute('rel', 'http://schemas.google.com/g/2005#work');
$email->setAttribute('address', $workEmail);
$entry->appendChild($email);
$contact = $doc->createElement('gd:phoneNumber', $workPhone);
$contact->setAttribute('rel', 'http://schemas.google.com/g/2005#work');
$entry->appendChild($contact);
$workAddress = $doc->createElement('gd:structuredPostalAddress');
$workAddress->setAttribute('rel', 'http://schemas.google.com/g/2005#work');
$entry->appendChild($workAddress);
$workStreet = $doc->createElement('gd:street', $workAddress1);
$workAddress->appendChild($workStreet);
$workPo = $doc->createElement('gd:pobox', $workAddress2);
$workAddress->appendChild($workPo);
$workXmlCity = $doc->createElement('gd:city', $workCity);
$workAddress->appendChild($workXmlCity);
$workXmlState = $doc->createElement('gd:region', $workState);
$workAddress->appendChild($workXmlState);
$workXmlZip = $doc->createElement('gd:postcode', $workZip);
$workAddress->appendChild($workXmlZip);
$wFormatted = $workAddress1 . $workCity;
$workFormatted = $doc->createElement('gd:formattedAddress', $wFormatted);
$workAddress->appendChild($workFormatted);
$xmlToSend = $doc->saveXML();
echo '<p>Making call</p>';
$client = GoogleHelper::getClient();
$req = new \Google_Http_Request('https://www.google.com/m8/feeds/contacts/default/full');
$req->setRequestHeaders(
array('content-type' => 'application/atom+xml; charset=UTF-8; type=feed'));
//,'GData-Version' => '3.0'));
$req->setRequestMethod('POST');
$req->setPostBody($xmlToSend);
$val = $client->getAuth()->authenticatedRequest($req);
$response = $val->getResponseBody();
正如您在上面所看到的,我已经注释了我尝试添加GData-Version标头的位置,因为这样做导致没有任何保存。
这是请求正文的样子:
<?xml version="1.0"?>
<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>address test</title>
<gd:email rel="http://schemas.google.com/g/2005#work" address="work@working.com"/>
<gd:phoneNumber rel="http://schemas.google.com/g/2005#work">123-123-1234</gd:phoneNumber>
<gd:structuredPostalAddress rel="http://schemas.google.com/g/2005#work">
<gd:street>123 Fake St</gd:street>
<gd:pobox>333</gd:pobox>
<gd:city>Minneapolis</gd:city>
<gd:region>MN</gd:region>
<gd:postcode>55427</gd:postcode>
<gd:formattedAddress>123 Fake StMinneapolis</gd:formattedAddress>
</gd:structuredPostalAddress>
</atom:entry>