Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

How to do everything with web 2.0
Nội dung xem thử
Mô tả chi tiết
CHAPTER 16: Use the Flickr API 247
The bottom of the API Explorer page is shown in Figure 16-6. The last two arguments are
useful for testing (and for implementation, too). You can limit the number of photo returns per
page (that is, in the photos XML element). You can also specify which page to retrieve. Note,
the page argument does not specify the number of pages to retrieve, but which of the various
computed pages is retrieved.
FIGURE 16-4 Use API Explorer
FIGURE 16-5 Go to the API Explorer page for flickr.photos.search
16
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
248 How to Do Everything with Web 2.0 Mashups
These arguments are specific to flickr.photos.search. At the bottom of the list, you specify
how you want to sign the request, and then you can click the Call Method button. If a request
does not need authentication, you do not need to sign it, so use the Do not sign call option, as
shown in Figure 16-6.
When you call the method, the area beneath the button is updated to show you the results of
the call, as shown in Figure 16-7. This is similar to the example response you saw on the basic
API page, but this is the actual result of the call you just generated. You can change argument
values and call the method over and over to see how it behaves. At the bottom of the page, the
actual call to the method is shown. You can copy and paste it into your code or use it as the base
of a function in PHP.
Here is a sample function to create a Flickr call, such as the one shown here. It breaks up the
construction of the call into several readable lines, which you can use exactly as shown here. As
indicated, a variable $theKeywords is used—just as it was used in the previous chapters—to pick
up data stored in the form’s request. This is passed into this function’s call. Also, the final link
specifies the number of photos per page and the page to display. You can omit this line if you
want.
function createFlickrCall ($theKeywords) {
$theCall = "http://api.flickr.com/services/rest/?";
$theCall .= "method=flickr.photos.search&api_key=";
// flickr Key is defined in PageTop.html
$theCall .= flickrKey;
$theCall .= "&tags=";
FIGURE 16-6 Set the signing options and call the method
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
CHAPTER 16: Use the Flickr API 249
// customize the call with the parameter $theKeywords
$theCall .= $theKeywords;
// omit or customize for the amount of data to be returned
$theCall .= "&per_page=5&page=1";
return $theCall;
}
You can visually parse the results (which is one reason for limiting the number of photos
returned, but beware of extrapolating from the minimal list of only one photo—always try to use
at least two or three).
The basic element of the response is an rsp element with a stat (status) attribute. Within that
is the photos element, representing the page requested (or all photos if there is only one). Within
the photos element are the photo elements, each representing a single photo.
The structure here begins to differ from the Amazon structure described in the last two
chapters. Part of the results of an Amazon search are shown formatted in Firefox in Figure 16-8.
FIGURE 16-7 Results of test flickr.photos.search call
16
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com