retrieve all contacts from a group in PHP

458 weergaven
Naar het eerste ongelezen bericht

Greg Levine

ongelezen,
20 mrt 2009, 23:51:2720-03-2009
aan Google Contacts API
I am trying to extract all contacts from a specified group, with the
goal of firing off an e-mail to each address in the group. I'm
starting by displaying a page that lists the authenticated user's
groups. When the user clicks on a group, he/she will see a form in
which the e-mail content will be typed. Upon submitting the e-mail,
each of the contacts in the selected group will be sent the e-mail
using PHP's mail function.

I've successfully authenticated a user using AuthSub and am able to
produce a list of contacts by querying "http://www.google.com/m8/feeds/
contacts/default/full" with the Zend_Gdata_Query method. However, I am
unable to produce a list of the groups associated with the
authenticated account. The code I am trying is as follows:

$gdata = new Zend_Gdata( $httpClient );
$query = new Zend_Gdata_Query( "http://www.google.com/m8/feeds/groups/
default/full" );
$query->setMaxResults( 100 );
$feed = $gdata->getFeed( $query );

foreach( $feed as $entry )
{
$xml= $entry->getXML();
print_r( $xml );
}

I don't see any errors. Just a blank page. Nothing is returned. Any
ideas as to why it might be failing?

Thanks in advance.

Greg

Greg Levine

ongelezen,
21 mrt 2009, 02:29:5321-03-2009
aan Google Contacts API
Sorry for cluttering up this discussion group. I was able to figure
out the answer.

It turns out that I only need to specify version 2 of the API in the
query.

$query = new Zend_Gdata_Query( "http://www.google.com/m8/feeds/groups/
default/full?v=2" );

And once I was using version 2, I had to remove the setMaxResults
line. That did it.

On another note, does anyone know how to get the e-mail address and/or
nickname of the authenticated user when using AuthSub?

Thanks again,

Greg

Greg Levine

ongelezen,
23 mrt 2009, 11:25:2523-03-2009
aan Google Contacts API
Okay, it's been two days of trying a number of different things to
make this work, but I can't seem to figure it out. (Note: I'm using
PHP.)

I'm able to retrieve a list of groups without any trouble. Then, using
a group ID that is returned, I want to query for the contacts in that
group. Let's say that group ID is http://www.google.com/m8/feeds/groups/user%40gmail.com/base/6.
I may need to do some work on the URI to make sure the query string is
properly formed, but the code I have (so far) is:

try
{
$gdata = new Zend_Gdata( $httpClient );
$query = new Zend_Gdata_Query( "http://www.google.com/m8/feeds/
contacts/user%40gmail.com/full/?group=6&v=2" ); // I've tried it
without the "v" query parameter too.
$feed = $gdata->getFeed( $query );
print_r( feed );
}
catch( Exception $e )
{
echo( $e->getMessage() );
}

It would seem that would work because I can replace the
Zend_Gdata_Query parameter with "http://www.google.com/m8/feeds/
contacts/user%40gmail.com/full" and print a complete list of my
contacts, without any restraints or restrictions. So, I'm not really
sure what I'm doing differently. Or, what I'm supposed to do
differently to print a list of contacts from a specific group. I've
spent a lot of time, specifically with
http://code.google.com/apis/contacts/docs/2.0/reference.html#Parameters,
trying to come up with a solution. But, it's been to no avail. Using
the above code, I keep receiving "Expected response code 200, got 400
Invalid value for group parameter: 6" as the exception. Using
variations, I've also received "404 Group not found" on occasion. Any
help is much is appreciated.

Thanks,

Greg

cwood

ongelezen,
23 mrt 2009, 11:55:2123-03-2009
aan Google Contacts API
Greg-

Try using the whole group id (url-encoded) in your query, like this:

http://www.google.com/m8/feeds/contacts/user%40gmail.com/full/?group=http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2Fgroups%2Fuser%40gmail.com%2Fbase%2F6&v=2

Regards,
Charlie
Spanning Sync


On Mar 23, 10:25 am, Greg Levine <levine.g...@gmail.com> wrote:
> Okay, it's been two days of trying a number of different things to
> make this work, but I can't seem to figure it out. (Note: I'm using
> PHP.)
>
> I'm able to retrieve a list of groups without any trouble. Then, using
> a group ID that is returned, I want to query for the contacts in that
> group. Let's say that group ID ishttp://www.google.com/m8/feeds/groups/user%40gmail.com/base/6.
> I may need to do some work on the URI to make sure the query string is
> properly formed, but the code I have (so far) is:
>
> try
> {
>         $gdata = new Zend_Gdata( $httpClient );
>         $query = new Zend_Gdata_Query( "http://www.google.com/m8/feeds/
> contacts/user%40gmail.com/full/?group=6&v=2" ); // I've tried it
> without the "v" query parameter too.
>         $feed = $gdata->getFeed( $query );
>         print_r( feed );}
>
> catch( Exception $e )
> {
>         echo( $e->getMessage() );
>
> }
>
> It would seem that would work because I can replace the
> Zend_Gdata_Query parameter with "http://www.google.com/m8/feeds/
> contacts/user%40gmail.com/full" and print a complete list of my
> contacts, without any restraints or restrictions. So, I'm not really
> sure what I'm doing differently. Or, what I'm supposed to do
> differently to print a list of contacts from a specific group. I've
> spent a lot of time, specifically withhttp://code.google.com/apis/contacts/docs/2.0/reference.html#Parameters,

Greg Levine

ongelezen,
23 mrt 2009, 14:04:5823-03-2009
aan Google Contacts API
Boy do I feel stupid! That just makes too much sense. I can't believe
it never occurred to me to try that. Thank you very much Charlie.

Greg

On Mar 23, 11:55 am, cwood <charlie.w...@gmail.com> wrote:
> Greg-
>
> Try using the whole group id (url-encoded) in your query, like this:
>
> http://www.google.com/m8/feeds/contacts/user%40gmail.com/full/?group=...
Bericht is verwijderd

cwood

ongelezen,
23 mrt 2009, 15:28:0223-03-2009
aan Google Contacts API
No problem. And trust me, I've gotten hung up on things simpler than
this. :-)

-c

darie nicolae

ongelezen,
23 mrt 2009, 18:18:5523-03-2009
aan Google Contacts API
Hello, i have one question regarding the same topic. After i made a
query to get the groups i got something like this :

http://www.google.com/m8/feeds/groups/user%40gmail.com/base/2842a9b10e7d48bb

Now in order to get the contacts from this group, the query needs to
be :

$query = new Zend_Gdata_Query("http://www.google.com/m8/feeds/groups/
default/full/?group=http://www.google.com/m8/feeds/groups/user
%40gmail.com/base/2842a9b10e7d48bb");

or it should be directly : Zend_Gdata_Query("http://www.google.com/m8/
feeds/groups/user%40gmail.com/base/2842a9b10e7d48bb"); ?

I dont get any response in both ways.


On Mar 23, 5:55 pm, cwood <charlie.w...@gmail.com> wrote:
> Greg-
>
> Try using the whole group id (url-encoded) in your query, like this:
>
> http://www.google.com/m8/feeds/contacts/user%40gmail.com/full/?group=...

Greg Levine

ongelezen,
24 mrt 2009, 10:24:3624-03-2009
aan Google Contacts API
darie nicolae,

You're just a little off with your contacts query. All you need to do
is change "groups" to "contacts" in the URI. When you query for
groups, you are telling the API that you want to look for a groups
feed. To query for contacts, you need to tell the API that you want to
look for a contacts feed. You should leave the group query parameter
URI exactly as is.

Also, note that I had more success with version 2 requests. For one
thing, I think the groups query required version 2. And for another,
you don't need to specify max results, since that property doesn't
seem to exist in version 2.

Best of luck,
Greg

On Mar 23, 6:18 pm, darie nicolae <darie.nico...@gmail.com> wrote:
> Hello, i have one question regarding the same topic. After i made a
> query to get the groups i got something like this :
>
> http://www.google.com/m8/feeds/groups/user%40gmail.com/base/2842a9b10...

darie nicolae

ongelezen,
24 mrt 2009, 10:35:1624-03-2009
aan Google Contacts API
Hey Greg,

Thanks for replying so fast. By version 2 you mean the version two of
the Zend Gdata?

Greg Levine

ongelezen,
24 mrt 2009, 13:19:4624-03-2009
aan Google Contacts API
Sorry for not clarifying. If you just recently downloaded Zend Gdata,
then you should have the latest version. You can read about specifying
a version at http://code.google.com/apis/contacts/docs/2.0/developers_guide_protocol.html#Versioning.

I read "every request that you send using the Contacts Data API should
specify version 2 of the API" to mean that while all functionality of
version 1 remains in version 2, you need to specify that you are using
version 2 in order to take advantage of the enhancements the most
recent version provides. If anyone out there knows better than me (and
I'm sure there are plenty), then please correct me if I'm wrong.

While the HTTP header method is preferred, I wasn't doing anything too
complex. So, I just tacked "v=2" onto the end of all of my query
URI's. In your case, it would look like the following:

$query = new Zend_Gdata_Query( "http://www.google.com/m8/feeds/
contacts/default/full?group=http://www.google.com/m8/feeds/groups/user
%40gmail.com/base/2842a9b10e7d48bb&v=2" );

Notice the "&v=2" after the group query string value? That's all it
takes to specify version 2.

Greg

darie nicolae

ongelezen,
24 mrt 2009, 13:53:0824-03-2009
aan Google Contacts API
Yep everything is clear, my next question is :

I did the query, this is the code :


$query = new Zend_Gdata_Query("http://www.google.com/m8/feeds/
contacts/" . $username . "/full/?group=" . $grouplink . '&v=2');
$feed = $gdata->getFeed( $query );

$xml = new SimpleXMLElement($feed->getXML());
$entries = $xml->children('http://www.w3.org/2005/Atom');

foreach ($entries->entry as $entry ) {

print_r($entry);

The array of entry contains only the name of the contact, but not the
address email and the other details.
Should they be returned on this query, or i need to do another query
based on the contacts name?

Thanks for your help.

Regards,
Darie Nicolae

On Mar 24, 7:19 pm, Greg Levine <levine.g...@gmail.com> wrote:
> Sorry for not clarifying. If you just recently downloaded Zend Gdata,
> then you should have the latest version. You can read about specifying
> a version athttp://code.google.com/apis/contacts/docs/2.0/developers_guide_protoc....

Greg Levine

ongelezen,
24 mrt 2009, 15:02:4824-03-2009
aan Google Contacts API
Yes, all of the contact data is returned with the feed. I think you
actually want to run your main foreach routine on $feed, not the XML
children. As I understand it, getFeed returns an array of strings
containing XML, and each element of the array is its own XML string
for an individual contact. So, you have to extract from the array each
XML string describing a contact before you convert a string into an
XML object. Here's how my code is structured:

$feed = $gdata->getFeed( $query );

foreach( $feed as $entry )
{
$xml = new SimpleXMLElement( $entry->getXML() );

. . .
}

Also, I'm using an XML-to-Array parser that I found last year in place
of SimpleXML. I would recommend sticking with SimpleXML since it is a
legitimate extension of PHP, but in case you want to see the
alternative, you can find it at http://mysrc.blogspot.com/2007/02/php-xml-to-array-and-backwards.html.

Hope that helps.

Greg

darie nicolae

ongelezen,
24 mrt 2009, 16:25:2024-03-2009
aan Google Contacts API
Ok greg, i did as you said

if i do print_r($xml) i get this :

SimpleXMLElement Object ( [edited] => 2009-03-23T21:51:40.519Z [email]
=> SimpleXMLElement Object ( [@attributes] => Array ( [rel] =>
http://schemas.google.com/g/2005#other [address] => ds...@dasda.com
[primary] => true ) ) [groupMembershipInfo] => Array ( [0] =>
SimpleXMLElement Object ( [@attributes] => Array ( [deleted] => false
[href] => http://www.google.com/m8/feeds/groups/varzariu.nicolae%40gmail.com/base/6
) ) [1] => SimpleXMLElement Object ( [@attributes] => Array
( [deleted] => false [href] =>
http://www.google.com/m8/feeds/groups/varzariu.nicolae%40gmail.com/base/2842a9b10e7d48bb
) ) ) ) SimpleXMLElement Object ( [edited] => 2009-03-23T21:36:09.859Z
[email] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] =>
http://schemas.google.com/g/2005#other [address] => hai...@yahoo.com
[primary] => true ) ) [groupMembershipInfo] => Array ( [0] =>
SimpleXMLElement Object ( [@attributes] => Array ( [deleted] => false
[href] => http://www.google.com/m8/feeds/groups/varzariu.nicolae%40gmail.com/base/6
) ) [1] => SimpleXMLElement Object ( [@attributes] => Array
( [deleted] => false [href] =>
http://www.google.com/m8/feeds/groups/varzariu.nicolae%40gmail.com/base/2842a9b10e7d48bb
) ) ) )

Now this $xml i need to parse it as array to get the values right? How
can i do that?

Thanks a lot for helping.

On Mar 24, 9:02 pm, Greg Levine <levine.g...@gmail.com> wrote:
> Yes, all of the contact data is returned with the feed. I think you
> actually want to run your main foreach routine on $feed, not the XML
> children. As I understand it, getFeed returns an array of strings
> containing XML, and each element of the array is its own XML string
> for an individual contact. So, you have to extract from the array each
> XML string describing a contact before you convert a string into an
> XML object. Here's how my code is structured:
>
> $feed = $gdata->getFeed( $query );
>
> foreach( $feed as $entry )
> {
>      $xml = new SimpleXMLElement( $entry->getXML() );
>
>      . . .
>
> }
>
> Also, I'm using an XML-to-Array parser that I found last year in place
> of SimpleXML. I would recommend sticking with SimpleXML since it is a
> legitimate extension of PHP, but in case you want to see the
> alternative, you can find it athttp://mysrc.blogspot.com/2007/02/php-xml-to-array-and-backwards.html.
> ...
>
> read more »

Greg Levine

ongelezen,
24 mrt 2009, 16:59:1224-03-2009
aan Google Contacts API
Darie Nicolae,

It would seem to me that you don't have an array of values after the
line "$xml = new SimpleXMLElement( $entry->getXML() )", you actually
have SimpleXMLElement object. It only prints like an array when using
print_r because that's the way PHP prints a SimpleXMLElement variable.
Another way of checking this is to use echo( var_dump( $xml ) );. That
prints the type and value of the variable.

Since you have a SimpleXMLElement object, you want to use the
functions at http://us.php.net/manual/en/ref.simplexml.php to parse
the SimpleXMLElement object. The functions "getName", "attributes" and
"children" can be used to trace through the XML doc and extract the
relevant data.

You're at the point where the purpose of your program, and thus what
you do with the returned data, is likely to be very different from
mine. Now that you have the data stored in an object (and you know
there is contact data in there because it printed with print_r), you
don't really need the Google Contacts API to print the information.
There's plenty of documentation on php.net, plus tons of tutorials
around the web, so give it a go.

Good luck,

Greg

On Mar 24, 4:25 pm, darie nicolae <darie.nico...@gmail.com> wrote:
> Ok greg, i did as you said
>
> if i  do print_r($xml) i get this :
>
> SimpleXMLElement Object ( [edited] => 2009-03-23T21:51:40.519Z [email]
> => SimpleXMLElement Object ( [@attributes] => Array ( [rel] =>http://schemas.google.com/g/2005#other[address] => d...@dasda.com
> [primary] => true ) ) [groupMembershipInfo] => Array ( [0] =>
> SimpleXMLElement Object ( [@attributes] => Array ( [deleted] => false
> [href] =>http://www.google.com/m8/feeds/groups/varzariu.nicolae%40gmail.com/ba...
> ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array
> ( [deleted] => false [href] =>http://www.google.com/m8/feeds/groups/varzariu.nicolae%40gmail.com/ba...
> ) ) ) ) SimpleXMLElement Object ( [edited] => 2009-03-23T21:36:09.859Z
> [email] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] =>http://schemas.google.com/g/2005#other[address] => hai...@yahoo.com
> [primary] => true ) ) [groupMembershipInfo] => Array ( [0] =>
> SimpleXMLElement Object ( [@attributes] => Array ( [deleted] => false
> [href] =>http://www.google.com/m8/feeds/groups/varzariu.nicolae%40gmail.com/ba...
> ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array
> ( [deleted] => false [href] =>http://www.google.com/m8/feeds/groups/varzariu.nicolae%40gmail.com/ba...
> ...
>
> read more »
Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten