Closed kaveiros closed 6 years ago
I don't know if I'm doing it correctly or not, but this seems to work for me. Just ensure $postalAddress
is passed into create()
$address = $doc->createElement('gd:postalAddress', $postalAddress);
$address->setAttribute('rel', 'http://schemas.google.com/g/2005#work');
$entry->appendChild($address);
@Tyxiquale is right. I believe that should do the trick.
To add entire address use the following code
$address = $doc->createElement('gd:structuredPostalAddress');
$address->setAttribute('rel','http://schemas.google.com/g/2005#work');
$city = $doc->createElement('gd:city',$city);
$address->appendChild($city);
$street = $doc->createElement('gd:street',$street);
$address->appendChild($street);
$region = $doc->createElement('gd:region',$region);
$address->appendChild($region);
$postcode = $doc->createElement('gd:postcode',$post_code);
$address->appendChild($postcode);
$country = $doc->createElement('gd:country',$country);
$address->appendChild($country);
$entry->appendChild($address);
By using the default fields the function is working fine. But by adding the above code I get exception.
exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_client", "error_description" : "The OAuth client was not found." }'
Here is the function so far
public static function create($name, $phoneNumber, $emailAddress, $city, $street, $region, $post_code, $country, $accessToken, $refreshToken, $clientId, $clientSecret) { $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'); $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gd', 'http://schemas.google.com/g/2005'); $doc->appendChild($entry);
$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', $emailAddress);
$entry->appendChild($email);
$contact = $doc->createElement('gd:phoneNumber', $phoneNumber);
$contact->setAttribute('rel', 'http://schemas.google.com/g/2005#work');
$entry->appendChild($contact);
//Additional fields
$address = $doc->createElement('gd:structuredPostalAddress');
$address->setAttribute('rel','http://schemas.google.com/g/2005#work');
$city = $doc->createElement('gd:city',$city);
$address->appendChild($city);
$street = $doc->createElement('gd:street',$street);
$address->appendChild($street);
$region = $doc->createElement('gd:region',$region);
$address->appendChild($region);
$postcode = $doc->createElement('gd:postcode',$post_code);
$address->appendChild($postcode);
$country = $doc->createElement('gd:country',$country);
$address->appendChild($country);
$entry->appendChild($address);
$xmlToSend = $doc->saveXML();
$client = GoogleHelper::getClient($accessToken, $refreshToken, $clientId, $clientSecret);
$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'));
$req->setRequestMethod('POST');
$req->setPostBody($xmlToSend);
$val = $client->getAuth()->authenticatedRequest($req);
$response = $val->getResponseBody();
$xmlContact = simplexml_load_string($response);
$xmlContact->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$xmlContactsEntry = $xmlContact;
$contactDetails = array();
$contactDetails['id'] = (string) $xmlContactsEntry->id;
$contactDetails['name'] = (string) $xmlContactsEntry->title;
foreach ($xmlContactsEntry->children() as $key => $value) {
$attributes = $value->attributes();
if ($key == 'link') {
if ($attributes['rel'] == 'edit') {
$contactDetails['editURL'] = (string) $attributes['href'];
} elseif ($attributes['rel'] == 'self') {
$contactDetails['selfURL'] = (string) $attributes['href'];
}
}
}
$contactGDNodes = $xmlContactsEntry->children('http://schemas.google.com/g/2005');
foreach ($contactGDNodes as $key => $value) {
$attributes = $value->attributes();
if ($key == 'email') {
$contactDetails[$key] = (string) $attributes['address'];
} else {
$contactDetails[$key] = (string) $value;
}
}
return new Contact($contactDetails);
}
Any suggestions?
I would recommend trying out the PHP Google People API package which supports adding locations to contacts.
Hi , thank you for this wonderful library!! I want to add location when I create a contact. Is this achievable ? Do you know the appropriate rel attribute and gd: element that are needed in order to do so?
Thank you in advance.