Plug in PHP 100 POWER SOLUTIONS- P61 pot

5 176 0
Plug in PHP 100 POWER SOLUTIONS- P61 pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

266 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 266 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s $title String containing the current title $temp Array used to extract the publishing site from the title $site String containing the current publisher of the story $desc String containing the current description/summary $percent Integer representing how similar one title is to another How It Works This program starts by initializing $reports, the array that will hold all the news reports. Then $url is assigned the location of the Yahoo! News RSS feed for the search term in $search—after $search has been converted to a form that can be passed in a URL using the rawurlencode() function. The feed is then called up using the file_get_contents() function, preceded by an @ symbol to suppress any error messages. The result is then placed in $xml. If it is FALSE or an empty string, then a single-element array containing the value FALSE is returned. Then, because the SimpleXML routines to be used later don’t appear to work with CDATA (XML character data; see plug-in 76, Search Yahoo!, for details), the next few lines remove the CDATA tags and convert characters that might clash with SimpleXML into entities it understands. Finally, $xml is converted into a SimpleXML object using the simplexml_load_string() function and the result is stored in $sxml. Next, a foreach loop is used to iterate through all the elements of $sxml->channel- >item, assigning each in turn to the object $item. Inside the loop, the first thing that happens is the variable $flag is set to FALSE. If it is later set to TRUE, then a title was found that was too similar to a previous one. The variable $url is then extracted and the string $date is created from a timestamp by using the strtotime() and date() functions. After that, the title and publishing site name are extracted into $title and $site, after exploding the title into the array $temp to split $site out of the title, where it was stored inside a pair of brackets. The news story is then saved into the variable $desc. To prevent similar stories being returned, a for loop is then used to iterate through all the saved stories in the $reports array. Using the similar_text() function, each previous title is compared to the current one and, if it is more than 70 percent similar, the variable $flag is set to TRUE and a break command is issued to break out of the loop, as no further duplication checking is necessary. At the tail end of the loop, as long as $flag doesn’t have a value of TRUE and $desc actually contains some text, the story parts are grouped into an array that is then assigned to the next available element of $reports. The plug-in returns a two-element array in which the first element is the number of news stories returned and the second is the $reports array. How to Use It To use this plug-in, you pass it a search term, like this: $search = "climate change"; $results = PIPHP_GetYahooNews($search); if (!$results[0]) echo "No news found for $search."; C h a p t e r 1 0 : A P I s , R S S , a n d X M L 267 C h a p t e r 1 0 : A P I s , R S S , a n d X M L 267 If $results[0] has the value FALSE or zero, then no stories were returned. Otherwise, you can access the stories in the following manner, which retrieves all the parts of the first story: $title = $results[1][0][0]; $site = $results[1][0][1]; $date = $results[1][0][2]; $story = $results[1][0][3]; $url = $results[1][0][4]; And the second result, like this (and so on): $title = $results[1][1][0]; $site = $results[1][1][1]; $date = $results[1][1][2]; $story = $results[1][1][3]; $url = $results[1][1][4]; The best way to display the results, though, is to use a foreach loop to iterate through each element of $results[1], placing each in another array such as $result (using the singular version of the variable name for single items extracted from the array), like this: foreach($results[1] as $result) echo "<a href='$result[4]'>$result[0]</a> ($result[1], " . "$result[2])<br />$result[3]<br /><br />"; In this example, each title in $result[0] is made the text of a hyperlink to the story’s original URL in $result[4], and the site and date in the variables $result[1] and $result[2] are displayed next to it in brackets. After a <br /> tag, the story in $result[3] is then displayed, followed by a couple more <br /> tags. To display Yahoo! News results to their best effect, you will probably also want to first echo or print a UTF-8 <meta> tag in the <head> section of your web page, so that any unusual characters display correctly. The correct meta tag looks like this: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> The Plug-in function PIPHP_GetYahooNews($search) { $reports = array(); $url = 'http://news.search.yahoo.com/news/rss?' . 'ei=UTF-8&fl=0&x=wrt&p=' . urlencode($search); $xml = @file_get_contents($url); if (!strlen($xml)) return array(FALSE); $xml = str_replace('<![CDATA[', '', $xml); $xml = str_replace(']]>', '', $xml); $xml = str_replace('&amp;', '[ampersand]', $xml); 268 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 268 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s $xml = str_replace('&', '&amp;', $xml); $xml = str_replace('[ampersand]', '&amp;', $xml); $xml = str_replace('<b>', '&lt;b&gt;', $xml); $xml = str_replace('</b>', '&lt;/b&gt;', $xml); $xml = str_replace('<wbr>', '&lt;wbr&gt;', $xml); $sxml = simplexml_load_string($xml); foreach($sxml->channel->item as $item) { $flag = FALSE; $url = $item->link; $date = date('M jS, g:ia', strtotime($item->pubDate)); $title = $item->title; $temp = explode(' (', $title); $title = $temp[0]; $site = str_replace(')', '', $temp[1]); $desc = $item->description; for ($j = 0 ; $j < count($reports) ; ++$j) { similar_text(strtolower($reports[$j][0]), strtolower($title), $percent); if ($percent > 70) { $flag = TRUE; break; } } if (!$flag && strlen($desc)) $reports[] = array($title, $site, $date, $desc, $url); } return array(count($reports), $reports); } Search Google Books As I write, as well as having already scanned in hundreds of thousands of out-of-copyright books, Google is in the process of making agreements with several book publishers over digitizing their in-copyright publications. This means that Google Books is likely to become an ever more useful research source that we can add to our toolkit. Figure 10-12 shows this plug-in being used to query the database for the term Mark Twain. About the Plug-in This plug-in takes a search query and returns matching books found in the Google Books database. Upon success it returns a two element array, the first of which is the number of 79 C h a p t e r 1 0 : A P I s , R S S , a n d X M L 269 C h a p t e r 1 0 : A P I s , R S S , a n d X M L 269 books returned and the second is an array containing details about those books. On failure it returns a single element array with the value FALSE. It requires these arguments: • $search A standard search query • $start The first result to return • $count The maximum number of results to return • $type The type of result to return. If this is 'none', then all books are returned; if 'partial', then books with partial previews are returned; or if 'full', then only books with full previews are returned. Variables, Arrays, and Functions $results Array containing returned book details $url String containing the Google Books API URL $xml String containing the result of loading in $url $sxml SimpleXML object created from $xml $title String containing the current book’s title $author String containing the current book’s author $pub String containing the current book’s publisher $date String containing the current book’s publication date $desc String containing the current book’s description/summary $thumb String containing the URL of the current book’s cover thumbnail $info String containing the URL of the current book’s information $preview String containing the URL for previewing the current book FIGURE 10-12 Add the vast resource of Google Book Search to your web site with this plug-in. 270 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 270 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s How It Works This plug-in starts off by initializing the array $results, which will be used to store the details of any books returned. Then $url is built up from the URL of the Google Books API, $search, converted into a form that can be passed in a URL using rawurlencode(), and the values of $start, $count, and $type. The result of calling this URL using file_get_contents(), prefaced by an @ symbol to suppress error messages, is placed in the string variable $xml, which is then passed to simplexml_load_string() to be converted into the object $sxml. But, just before this, all occurrences of the string dc: in $xml have the colon removed for the benefit of the SimpleXML routines, which don’t seem to like colons in XML field names. The $sxml object is then iterated through using a foreach loop, with each element in $sxml->entry being assigned to the object $item for ease of access. The title, author, publisher, date, and description are then all retrieved and placed in the variables $title, $author, $pub, $date, and $desc. There are then three URLs to fetch: the thumbnail image, a link to the information page at Google Books, and the link to the preview page at Google Books. These are placed in $thumb, $info, and $preview. Next a few bits of sorting out need to occur. First, if $pub doesn’t have a value, then the value in $author is given to it. Similarly, if $preview is found to not link to an actual preview of the book, then it is set to FALSE. If the description in $desc is missing, it is assigned the value (No description), and if it is determined that there is no thumbnail image specific to this book, a link to a generic cover image at Google Books is assigned to $thumb. At the tail end of the loop, all these items of information are grouped together into an array, which is then assigned to the next available element in $results. The plug-in returns a two-element array in which the first element is the number of books returned and the second is the $results array. How to Use It To use this plug-in, pass it a search query and arguments telling it which number result to start returning details from, the maximum number of results, and the type of results. For example, to return up to 20 books relating to the search Mark Twain, starting at the first result, and where any or no summary is available, you would use code such as this: $search = "Mark Twain"; $result = PIPHP_SearchGoogleBooks($search, 1, 20, 'none'); if (!$result[0]) echo "No books found for $search."; If $result[0] is FALSE or zero, then no results were returned. Otherwise, the details returned for the first book will be in the array $result[1] and can be accessed like this: $title = $result[1][0][0]; $author = $result[1][0][1]; $publisher = $result[1][0][2]; $date = $result[1][0][3]; $description = $result[1][0][4]; $thumbnail = $result[1][0][5]; $information = $result[1][0][6]; $preview = $result[1][0][7]; . containing returned book details $url String containing the Google Books API URL $xml String containing the result of loading in $url $sxml SimpleXML object created from $xml $title String containing. date $desc String containing the current book’s description/summary $thumb String containing the URL of the current book’s cover thumbnail $info String containing the URL of the current book’s information $preview String. s $title String containing the current title $temp Array used to extract the publishing site from the title $site String containing the current publisher of the story $desc String containing the

Ngày đăng: 07/07/2014, 08:20

Mục lục

    1 Building a Development Server

    Windows XP, Windows Vista, and Windows 7

    Reinstalling Zend Server CE

    Upgrading Zend Server CE

    Ubuntu and Debian Linux

    Fedora, RHEL, and CentOS Linux

    Other Versions of Linux

    Mac OS X 10.4 Plus on Intel Chips

    Configuring Error Handling in Zend Server CE

    And Now You’re Set to Go

Tài liệu cùng người dùng

Tài liệu liên quan