
| Tip: Perform trackbacks with PHP | ||||||||||||||||||||||||||
| 摘自: IBM developerWorks Worldwide 被阅读次数: 43 | ||||||||||||||||||||||||||
由 yangyi 于 2008-06-25 10:40:42 提供 | ||||||||||||||||||||||||||
Level: Intermediate Vikram Vaswani, Founder, Melonfire 25 Mar 2008 Trackbacks play an important role in allowing blog readers to find and follow links to useful related content. The PEAR Services_Trackback package lets developers manually generate these trackbacks through a well-defined PHP API that takes care of creating trackback requests and parsing XML responses. One of the nice things about the blogosphere is how highly-networked and rapidly-evolving a community it is. By posting useful content and linking it to relevant material posted by other bloggers, an individual blogger can rapidly make his presence felt in the community and even build a fan base in a fraction of the time it would take in the real world.
Among the various tools used by bloggers to link posts together is the trackback, which provides a mechanism for one blogger to tell another of a related post or resource through a backlink. Trackbacks play an important role in strengthening the inter-connections between blogs, and also provide a convenient (and automatic) way for blog readers to find out about useful, related content. If you already have a blog, chances are that you can use the built-in features of your blog to create trackbacks to content on other blogs. But what if you don't have a blog, but still want to create trackbacks between your Web posts and other blogs? Well, if you use PHP, you can benefit from the Services_Trackback package, which provides a generic mechanism to manually create and send trackbacks.
The Services_Trackback package is maintained by Tobias Schlitt, and released to the PHP community under a PHP license. It is freely available from PEAR, the PHP Extension and Application Repository, and the easiest way to install it is with the automated PEAR installer, which should have been included by default with your PHP build. To install it, simply issue the following command at your shell prompt:
The PEAR installer will now connect to the PEAR package server, download the package, and install it to the appropriate location on your system. This tip uses Services_Trackback V0.6.1 (beta). To install the package by hand, visit its home page, download the source code archive, and manually uncompress the files to the desired location. Note that this manual installation process presupposes some knowledge of PEAR's package organization structure. You'll find a link to the package's home page in the Resources of this tip.
Before you dive into Services_Trackback, a few words about how trackbacks work are in order. The original Trackback specification was developed by SixApart, and it specifies that a trackback request is initiated by sending a POST request over HTTP to a specified trackback URL. This trackback URL is the URL defined by the source blog to receive trackback requests for a specific post. This POST request contains various bits of information to make the trackback more useful:
Of these pieces of information, only the URL of the page that initiates the trackback is mandatory; however, to create trackbacks that are relevant and useful, you should always include meaningful content for the other fields as well.
Listing 1 has an example of one such POST request, which creates a trackback to the original post at Listing 1: A POST request for a trackback
Now, when the destination server receives this trackback request, the server generates one of two responses, both formatted in XML. If the trackback request is in order, does not appear to be spam and the host initiating the trackback does not appear on a blacklist, the server will typically accept and create the trackback link, responding with the success message in Listing 2: Listing 2: An XML success response
If, on the other hand, the trackback fails, an error message like Listing 3 is generated: Listing 3: An XML error response
Fortunately, Services_Trackback does all the work to generate the POST request and decode the response for you. Listing 4 demonstrates typical usage of this package: Listing 4: Generating a trackback with PHP
Listing 4 uses Services_Trackback package in PHP to create
and post a trackback to a blog post. It begins by reading the Services_Trackback
class file and initializing an object instance of the Services_Trackback class.
Next, the
It's quite easy to use the Services_Trackback package in an interactive manner. You create a Web form that allows users to post trackbacks to any blog that supports them. Listing 5 demonstrates: Listing 5: A Web form to interactively set a trackback
Listing 5 generates a Web form that asks the user to input various attributes for a trackback: the trackback URL, post title and URL, blog name, and excerpt. This information is then submitted to a PHP script that initializes a new Services_Trackback object, and posts the trackback to the specified URL. The XML response is then decoded and a success or failure notification displayed. Figure 1 illustrates what the Web form looks like: Figure 1. A Web form to interactively generate a trackback
As this example illustrates, the Services_Trackback package provides an easy way to manually create trackbacks to blog post requests. It's very useful when you need to generate a trackback to your content, but don't necessarily have a blog to do this with. Play with it some and see what you think! Learn
Get products and technologies
Discuss
Original link: http://www.ibm.com/developerwork... | ||||||||||||||||||||||||||