Hello, my name is Trevor Johns,
and I'm a software engineer from the Google Data APIs team.
The Google Data APIs allow you to programmatically access
various Google services,
such as Blogger, Google Calendar,
your Gmail contacts list, or even YouTube.
I'm here today to help you get started interacting
with these APIs from within your PHP applications.
This video assumes that you're already familiar
with the basics of web development and PHP.
It'll also help if you had past experience
working with web services,
though this isn't strictly required.
Before we begin, you'll need three things--
An HTTP Server, PHP,
and the PHP Google Data Client Library.
I'll go through each of these briefly.
Most hosting providers already provide an HTTP server and PHP,
but if you haven't already done so,
I highly recommend installing a copy locally
for development purposes.
Any HTTP server will do,
provided that there's support executing PHP scripts.
I recommend using the Apache HTTP Server,
which can be downloaded from httpd.apache.org.
Once your HTTP server is installed,
you can download PHP from php.net.
OS-specific installation instructions
are posted on each of these sites.
While installing Apache and PHP
are outside the scope of this video,
I'll show you how to quickly check to see
if your installation is properly set up.
Simply create a file that contains these two lines.
The first is just the standard PHP start tag,
and the second is the PHP info function,
which, if you've done PHP development before,
you might know that it goes and displays information
about your PHP environment.
If we save this within our web root
as, uh, say info.php...
and then load it within our browser...
it should show information about your PHP installation,
such as the version, any build commands you used,
what your include path is.
If you don't get this page and instead you just get
the contents of the info.php file you just saved,
then you know something's wrong, and you'll need to troubleshoot.
Once you have your HTTP server installed
and running with PHP support,
it's time to download the PHP Google Data Client Library.
This is distributed
as part of the Zend Framework Open Source Project,
available at framework.zend.com.
Zend Framework includes a lot of components
besides just what's needed for accessing the Google Data APIs.
So, if you don't want all the overhead of Zend Framework
or are already using another web-development toolkit,
you can download a lightweight Google-only version
of Zend Framework
by going to framework.zend.com/ download/gdata.
To demonstrate, I'll quickly go through
the Zend Framework installation process.
So I'm just gonna go ahead and open up a browser
and go to framework.zend.com/ download/gdata
and grab the tarball from Latest Release.
Once it finishes downloading,
I'm just gonna go ahead and extract it.
Once this is done, place the resulting folder
somewhere safe and accessible to your web server.
Since on my system,
both Apache and PHP are installed in opt/local,
I'm gonna go ahead and place it there as well.
On your system, you'll probably want to put it somewhere else.
Finally, you'll need to tell PHP where to find Zend Framework.
The best way to do this is to modify your php.ini file.
On my system, this is located in opt/local/etc
because I installed PHP into opt/local.
However, depending on how you've installed PHP,
it may be located in another location,
such as user/local/etc
or C:\PHP if you're on Windows.
So I'm gonna go ahead and open this up in Text Editor.
All right?
And find the line that begins with "include_path."
All right?
This is a colon-separated list of various directories
that PHP is going to search when it's looking up, um, libraries.
I'm just gonna go ahead
and add the path where I stored Zend Gdata,
so opt/local/ZendGdata followed by /library.
Now that I've updated my php.ini,
I just need to restart Apache
so that PHP can pick up the new changes.
So I'm just gonna go ahead and run Apache control restart.
And then it should be good to go.
So I'm just gonna go ahead and load php-a
to get an interactive PHP session,
and I'm going to try and load a file from Zend Framework.
I'm gonna load Zend/Loader.php,
which should be there.
If you don't get any errors, then everything's good to go.
If not, then you know you need to troubleshoot.
A good place to check, if this happens,
is to look at the info.php file we created earlier.
This will show you
the include path is currently in effect.
So, if this is wrong,
make sure that you've edited the correct php.ini file
and that the path you listed is actually correct.
If you're deploying to manage hosting,
you might have direct access to your php.ini file.
If you're hosting provider has allowed it
and if your hosting provider is using Apache with mod-php,
you can instead set PHP's configuration settings
using an htaccess file.
So to do this, go ahead and create a new file.
And then type the line, "php_value include_path"
and then in parentheses the path to your Zend Gdata folder
followed by /library.
Then save this in your web root as .htaccess
and save it.
I'm not gonna actually save it
because I've already edited my php.ini,
but otherwise, this should work.
Now that Zend Framework is installed,
let's take a brief tour of the client library.
A useful page for this is the Zend Framework API reference,
available at framework.zend.com/ apidoc/core.
Let's go there now.
All right, click on Zend_Gdata in the upper-right-hand corner
to switch to--to make that the active package.
And then you'll notice on the left there's a list of classes
within the-- within Zend Gdata.
There's five main types of classes
that you'll normally be interacting with--
service classes, query classes,
feeds, entries, and extension elements.
Zend Gdata is an example of a service class.
Service classes are the heart of the client library
and serve to manage access
to Google services over the network.
Zend Gdata is a generic service class used for Blogger.
Most of the services have a dedicated service class,
such as Zend Gdata at YouTube.
Query classes contain knowledge
about the address scheme used for the various services
and are used to assist in forming URLs.
You can either allow the query object to create a URL for you,
or just pass the URL into the query yourself if you know it.
As before, Zend Gdata Query is a generic query class,
though many services have specialized query classes,
such as Zend Gdata YouTube.
Feeds are returned by service classes
in response to a network request,
often using the help of a query.
Feeds are simply a collection of entry objects,
along with a small bit of metadata
to describe the collection.
Zend Gdata Feed is an example of this,
and there is many subclasses used for the various services.
Entries, as the name suggests,
represent a logical collection of data about something.
This might be a blog post
or a contact or a calendar event.
These are represented by Zend Gdata Entry,
and like Zend Gdata Feed,
there's many subclasses used by each of the services.
Finally, inside of entries and feeds, extension elements
are used to represent individual pieces of data.
For example, this might be the owner of a blog,
the title of a post, or the date of a calendar event.
These are represented by subclasses
of Zend Gdata App Extension.
Now that you're familiar with the library,
we're going to write a quick program
to create and display posts for Blogger.
Let's get started.
So, to begin, I'm gonna go and create
a brand-new HTML file.
I'll go ahead and call this, "Blogger PHP Demo."
And I'll go ahead and put a nice title at the top of the page.
All right?
And then I'm just gonna go ahead and save this as blogger.php
inside of my web root.
At the very top, I'll go ahead and insert a block of PHP code
to load in Zend Gdata libraries.
The very first thing you should always load
when using Zend Framework is Zend/Loader.php.
You can then use Zend Loader
to load in any other dependencies that you have.
In this case, we're using Zend Gdata,
Zend Gdata Query, and Zend Gdata ClientLogin.
Zend Gdata and Zend Gdata Query I discussed earlier.
Zend Gdata ClientLogin
is used to go and authenticate to Blogger.
So, once that's in place, I'll just go down here to the body,
create another block of PHP code,
and authenticate to Blogger.
So, again, we're using ClientLogin
to actually authenticate.
Uh, some other options you might want to use here
are auth 7.0 auth, which allow you to go and log in
without ever actually seeing the user's password,
which is really great for web apps,
since it protects the user's security.
Once that's done, we create an instance
of the Zend Gdata service class and call it Blogger Service.
From there, we're just gonna go ahead
and get a list of all the user's current blogs.
So we create a new query.
Um, this URL just happens to be the Blogger Meta Feed,
which lists all of the blogs available for a user.
This is documented on code.google.com.
And then we go and use the Blogger service
to go and fetch that feed.
From there, we just go and extract the first blogs,
since that's all we care about right now,
and we get the title, the blog's URL,
and the URL of the feed for that blog.
Something to note here is that title,
while you'd normally think of it as a string,
is actually an XML element,
and as such, it can have various attributes,
so you need to be sure
to actually request the text for the title,
as opposed to anything else that might be there.
And this applies to any other extension element as well.
All right?
And then we're just gonna go ahead and print this out.
So, if we load this in our browser...
you can see that it successfully fetched the first blog.
And if I load it, there's the index page,
and that looks good.
So let's go ahead
and do something a little more interesting.
Let's go and actually fetch
all the blog posts available on this blog.
So, again, we construct a new query,
and we use the blog feed URL that we extracted earlier.
We then again use the service to go and fetch this feed.
And then we iterate through every single entry in this feed,
where it--and here every entry represents a blog post.
For each blog post,
we go and extract the URL for the blog post
and the title
and display them on screen in a list.
So, if we refresh the page again...
you'll see it successfully fetched
all the blog posts available.
All right?
So, now that that's done,
let's go and insert some new posts into the blog.
So I'm just gonna go and create a form down here.
This is just a normal post
to the exact same page we're on right now.
It has two inputs--
a text field named "title"
and a text area named "body."
And you can see that right here.
Right now, this isn't wired up to do anything yet.
So let's fix that.
So, right after we fetch the first blog,
we'll check to see if there's anything in the forum.
If the forum's completely empty, we won't do anything,
but if there are things in the forum,
we'll go and, uh, create a brand-new entry,
populate the title and the content
using title and content extension elements,
and then just grab the title and the body from the post
and then use the service class to insert that into the blog,
using insert entry.
If we save this one more time
and then switch back to the browser--
I go ahead and type in a title here.
So, "Hello, World.
This is my sample post."
And hit continue.
You'll see that the post appears in the list.
If I reload the blog's index,
it appears there.
And if I click on the post right here,
it takes us to the page dedicated to that post.
If you'd like to learn more,
check out the PHP Getting Started Guide
that accompanies this video.
The URL is on your screen.
The Zend Framework Manual contains in-depth information
about working with Zend Framework modules,
including this one.
You can find this at framework.zend.com/manual.
Finally, be sure to keep
the Zend Framework API reference I mentioned earlier on hand,
available at framework.zend.com/ apidoc/core.
I hope you got a good idea how to get started.
Thank you for watching and happy coding.