rapidwebltd / php-google-contacts-v3-api

👥 PHP library for the Google Contacts API (v3)
MIT License
102 stars 64 forks source link

Add multiple phone types, phone numbers #32

Closed KiemDuong closed 6 years ago

KiemDuong commented 7 years ago

Hi there,

I'm using your API to sync Google contacts in my app. However, I need to add multiple phone type, phone number to a contact. Could you extent this library to support this. Thank you!

Example: Wife: +1988519007 Husband:+1988519234

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/41880488-add-multiple-phone-types-phone-numbers?utm_campaign=plugin&utm_content=tracker%2F19518826&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F19518826&utm_medium=issues&utm_source=github).
kuriban commented 7 years ago

Hi. I change function create. So you can create multiple phone type and multiple email $phoneNumber, $emailAddress - must be arrays

 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);

        foreach($emailAddress as $key=>$one_mail){
            $email = $doc->createElement('gd:email');
            if(!$key){
                $email->setAttribute('primary', 'true');
            }
            $email->setAttribute('rel','http://schemas.google.com/g/2005#work');
            $email->setAttribute('address',$one_mail);
            $entry->appendChild($email);
        }

        foreach($phoneNumber as $key=>$phone){
            $contact = $doc->createElement('gd:phoneNumber', $phone);
            if(!$key){
                $contact->setAttribute('primary', 'true');
            }
            $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'];
            } else {
                $contactDetails[$key] = (string) $value;
            }
        }

        return new Contact($contactDetails);
    } 
DivineOmega commented 7 years ago

It looks like we'll be able to change http://schemas.google.com/g/2005#work to http://schemas.google.com/g/2005#home or http://schemas.google.com/g/2005#other in order to submit multiple different types of phone numbers or email addresses.

kuriban commented 7 years ago

Ok. Can you solve a problem with update multiple count?

brianyoungblood commented 7 years ago

I have a similar question related to this. Is there a possible solution for updating multiple emails? ie. https://github.com/rapidwebltd/php-google-contacts-v3-api/issues/45

DivineOmega commented 6 years ago

Unfortunately, this library can't handle multiple email addresses, telephone numbers, etc.

We've recently written a new package, PHP Google People API, that uses an improved API to access Google Contacts information. This library is very capable of retrieving and updating multiple emails address, telephone numbers and so on.