public function processEntityRequest($io, $client) { $req = new Google_Http_Request("http://localhost.com"); $req->setRequestMethod("POST"); // Verify that the content-length is calculated. $req->setPostBody("{}"); $io->processEntityRequest($req); $this->assertEquals(2, $req->getRequestHeader("content-length")); // Test an empty post body. $req->setPostBody(""); $io->processEntityRequest($req); $this->assertEquals(0, $req->getRequestHeader("content-length")); // Test a null post body. $req->setPostBody(null); $io->processEntityRequest($req); $this->assertEquals(0, $req->getRequestHeader("content-length")); // Set an array in the postbody, and verify that it is url-encoded. $req->setPostBody(array("a" => "1", "b" => 2)); $io->processEntityRequest($req); $this->assertEquals(7, $req->getRequestHeader("content-length")); $this->assertEquals(Google_IO_Abstract::FORM_URLENCODED, $req->getRequestHeader("content-type")); $this->assertEquals("a=1&b=2", $req->getPostBody()); // Verify that the content-type isn't reset. $payload = array("a" => "1", "b" => 2); $req->setPostBody($payload); $req->setRequestHeaders(array("content-type" => "multipart/form-data")); $io->processEntityRequest($req); $this->assertEquals("multipart/form-data", $req->getRequestHeader("content-type")); $this->assertEquals($payload, $req->getPostBody()); }
public static function delete(Contact $toDelete) { $client = GoogleHelper::getClient(); $req = new \Google_Http_Request($toDelete->editURL); $req->setRequestHeaders(array('content-type' => 'application/atom+xml; charset=UTF-8; type=feed')); $req->setRequestMethod('DELETE'); $client->getAuth()->authenticatedRequest($req); }
public static function create($name, $phoneNumber, $emailAddress) { $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); $xmlToSend = $doc->saveXML(); $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')); $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']; } elseif ($key == 'organization') { $contactDetails[$key] = 'hello'; //$contactDetails[$key] = (string) $value->children()->current() ?: 'hello'; } else { $contactDetails[$key] = (string) $value; } } return new Contact($contactDetails); }
private static function _create_contact($client, $cid, $id, $name, $emailAddress, $phoneNumber, $industryGroup, $address, $comments, $company, $title, $birthday, $url, $referral, $manager, $workPhone, $cellPhone, $faxPhone) { $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'); $entry->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gContact', 'http://schemas.google.com/contact/2008'); $doc->appendChild($entry); if ($id !== false) { $idDom = $doc->createElement('id', $id); $entry->appendChild($idDom); } $categoryDom = $doc->createElement('category'); $categoryDom->setAttribute('term', 'user-tag'); $categoryDom->setAttribute('label', $industryGroup['title']); $entry->appendChild($categoryDom); $titleDom = $doc->createElement('title', $name); $entry->appendChild($titleDom); $contentDom = $doc->createElement('content', $comments); $entry->appendChild($contentDom); $emailDom = $doc->createElement('gd:email'); $emailDom->setAttribute('rel', 'http://schemas.google.com/g/2005#work'); $emailDom->setAttribute('address', $emailAddress); $entry->appendChild($emailDom); if (!empty($phoneNumber)) { $phoneNumberDom = $doc->createElement('gd:phoneNumber', $phoneNumber); $phoneNumberDom->setAttribute('rel', 'http://schemas.google.com/g/2005#main'); $phoneNumberDom->setAttribute('primary', 'true'); $entry->appendChild($phoneNumberDom); } if (!empty($workPhone)) { $phoneNumberDom = $doc->createElement('gd:phoneNumber', $workPhone); $phoneNumberDom->setAttribute('rel', 'http://schemas.google.com/g/2005#work'); $entry->appendChild($phoneNumberDom); } if (!empty($cellPhone)) { $phoneNumberDom = $doc->createElement('gd:phoneNumber', $cellPhone); $phoneNumberDom->setAttribute('rel', 'http://schemas.google.com/g/2005#mobile'); $entry->appendChild($phoneNumberDom); } if (!empty($faxPhone)) { $phoneNumberDom = $doc->createElement('gd:phoneNumber', $faxPhone); $phoneNumberDom->setAttribute('rel', 'http://schemas.google.com/g/2005#fax'); $entry->appendChild($phoneNumberDom); } if (!empty($address)) { $postalAddressDom = $doc->createElement('gd:postalAddress', $address); $postalAddressDom->setAttribute('rel', 'http://schemas.google.com/g/2005#work'); $postalAddressDom->setAttribute('primary', 'true'); $entry->appendChild($postalAddressDom); } if (!empty($company) || !empty($title)) { $orgDom = $doc->createElement('gd:organization'); $orgDom->setAttribute('rel', 'http://schemas.google.com/g/2005#work'); $orgDom->setAttribute('primary', 'true'); if (isset($company) && !empty($company)) { file_put_contents(dirname(__FILE__) . '/update.txt', PHP_EOL . 'Company: ' . PHP_EOL . var_export($company, true) . PHP_EOL, FILE_APPEND); $orgNameDom = $doc->createElement('gd:orgName', $company); $orgDom->appendChild($orgNameDom); } if (isset($title) && !empty($title)) { file_put_contents(dirname(__FILE__) . '/update.txt', PHP_EOL . 'Title: ' . PHP_EOL . var_export($title, true) . PHP_EOL, FILE_APPEND); $orgTitleDom = $doc->createElement('gd:orgTitle', $title); $orgDom->appendChild($orgTitleDom); } $entry->appendChild($orgDom); } if (!empty($referral)) { $referralDom = $doc->createElement('gd:extendedProperty'); $referralDom->setAttribute('name', 'referral'); $referralDom->setAttribute('value', $referral); $entry->appendChild($referralDom); } if (!empty($manager)) { $managerDom = $doc->createElement('gd:extendedProperty'); $managerDom->setAttribute('name', 'manager'); $managerDom->setAttribute('value', $manager); $entry->appendChild($managerDom); } if (!empty($birthday)) { $birthdayDom = $doc->createElement('gContact:birthday'); $birthdayDom->setAttribute('when', $birthday); $entry->appendChild($birthdayDom); } $birthdayDom = $doc->createElement('gContact:relation'); $birthdayDom->setAttribute('label', 'Ontraport Contact'); $entry->appendChild($birthdayDom); if (!empty($url)) { $websiteDom = $doc->createElement('gContact:website'); $websiteDom->setAttribute('href', $url); $websiteDom->setAttribute('rel', 'http://schemas.google.com/g/2005#profile'); $websiteDom->setAttribute('primary', 'true'); $entry->appendChild($websiteDom); } $groupMemDom = $doc->createElement('gContact:groupMembershipInfo'); $groupMemDom->setAttribute('href', $industryGroup['id']); $groupMemDom->setAttribute('deleted', 'false'); $entry->appendChild($groupMemDom); $xmlToSend = $doc->saveXML(); file_put_contents(dirname(__FILE__) . '/update.txt', PHP_EOL . 'Request XML' . PHP_EOL . $xmlToSend . PHP_EOL, FILE_APPEND); if ($cid !== false) { file_put_contents(dirname(__FILE__) . '/update.txt', PHP_EOL . '%%%% Priming UPDATE request header with CID: ' . $cid, FILE_APPEND); $req = new Google_Http_Request('https://www.google.com/m8/feeds/contacts/default/full/' . $cid); $req->setRequestHeaders(array('content-type' => 'application/atom+xml; charset=UTF-8; type=feed')); $req->setRequestMethod('PUT'); $req->setPostBody($xmlToSend); } else { file_put_contents(dirname(__FILE__) . '/update.txt', PHP_EOL . '++++ Priming CREATE request header', FILE_APPEND); $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); } file_put_contents(dirname(__FILE__) . '/update.txt', PHP_EOL . 'Making request', FILE_APPEND); $val = $client->getAuth()->authenticatedRequest($req); $response = $val->getResponseBody(); file_put_contents(dirname(__FILE__) . '/update.txt', PHP_EOL . PHP_EOL . '===RESPONSE===' . PHP_EOL . $response . PHP_EOL . PHP_EOL, FILE_APPEND); $xmlContact = simplexml_load_string($response); $xmlContact->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005'); return OPContactProxy::xml2array($xmlContact); }