Plug in PHP 100 POWER SOLUTIONS- P36 ppt

5 138 0
Plug in PHP 100 POWER SOLUTIONS- P36 ppt

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

Thông tin tài liệu

CHAPTER 7 The Internet 142 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 142 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 W hen you create a web site, rather than existing on its own, it becomes part of the wider Internet as a whole. This means people will interact with it in many ways, from bookmarking pages they like, to subscribing to RSS feeds. While other web sites may wish to exchange links with you to help pool and build traffic, T witter users may want to tweet about something on your site and so on. This chapter provides a range of plug-ins to help integrate your new property into the Internet community at large, including link management, creating short URLs, converting between HTML and RSS, adapting a site to mobile browsers and more. Check Links When building up a web site, and especially if the marketing budget is tight, you often have to embark on a campaign of link exchanges with other sites. But tracking all those link exchanges to ensure the other sites keep their end of the deal is time-consuming. However, with this plug-in you simply pass an array of one or more links which should be present on a particular web page and you will be informed whether those links are all in place. Figure 7-1 shows the plug-in in action; two of the links have passed, but a link to http://doesnotexist.com/ index.html is not found on the www.pluginphp.com index page. About the Plug-in This plug-in accepts the URL of a web page to check, along with a set of links that ought to be present on it. If all the links are present, it returns an array with the single value TRUE, otherwise it returns a two-element array, of which the first element is FALSE. The second is an array containing all the links that were not present. It takes these arguments: • $url A string containing the URL of a page to check • $links An array of links to look for on the page at $url FIGURE 7-1 This plug-in tests whether certain links are present on a particular web page. 4 1 C h a p t e r 7 : T h e I n t e r n e t 143 C h a p t e r 7 : T h e I n t e r n e t 143 Variables, Arrays, and Functions $results Array containing all the links found at $url $missing Array containing any links passed in $links that are not present in $results $failed Integer counter that indexes into $missing, incrementing on each failed match $link String containing the current link being processed, as extracted from $links PIPHP_GetLinksFromURL() Plug-in 22: This function returns all the links at a given URL. PIPHP_RelToAbsURL() Plug-in 21: This function converts a relative-to-absolute URL and is used by plug-in 22. How It Works The first thing this plug-in does is call plug-in 22, PIPHP_GetLinksFromURL(), to fetch all the links within the page supplied in the variable $url. All links found are then placed in the array $results. Next, a couple of variables are initialized, ready for checking whether these links include the ones being looked for. These are the array $missing, which will hold any links not found, and the integer $failed, which is set to zero and will be incremented when any link is determined to not be present. Then a foreach loop iterates through all the returned links, temporarily placing each in the string variable $link, where it is then checked against the array of all links in $results, using the function in_array(). Any that are not found are placed in $missing and the array pointer $failed is incremented. After the checking is complete, if any links are not found, then a two-element array is returned, the first element of which is FALSE, and the second is an array containing all the links that failed the check. Otherwise, a single-element array is returned, containing the value TRUE. How to Use It To use this plug-in, you should supply a URL to be checked and a list of links that should be included within the page at that URL, like this: $url = "http://pluginphp.com"; $links = array("http://pluginphp.blogspot.com", "http://lpmj.net", "http://doesnotexist.com/index.html"); $result = PIPHP_CheckLinks($url, $links); If the value in $result[0] is TRUE, then all the links are present and correct. But if $result[0] is FALSE, then $result[1] will contain an array of all failed links. You can check for these conditions using code such as the following, which employs a for loop to iterate through the failed links: if ($result[0]) echo "All links verified"; else { echo "<br /><b>Verification failed for:<br />"; for ($j = 0 ; $j < count($result[1]) ; ++$j) echo $result[1][$j] . "<br />"; } 144 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 144 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 Remember that because this plug-in also uses plug-ins 21 and 22, PIPHP_RelToAbsURL() and PIPHP_GetLinksFromURL(), you should also copy and paste them into your program, or otherwise include them. The Plug-in function PIPHP_CheckLinks($url, $links) { $results = PIPHP_GetLinksFromURL($url); $missing = array(); $failed = 0; foreach($links as $link) if (!in_array($link, $results)) $missing[$failed++] = $link; if ($failed == 0) return array(TRUE); else return array(FALSE, $missing); } Get Title from URL Sometimes you want to know what the title of a web page is. Using this plug-in in combination with the following one (plug-in 43), it’s possible to link back to a referring web page using its title. Figure 7-2 shows the title being fetched from the Yahoo! News home page. About the Plug-in This plug-in accepts the URL of a web page whose title is to be extracted and returns the title. It takes this argument: • $page A string containing the URL of a page to check FIGURE 7-2 When you need to know the title of a web page, you can call this plug-in. 42 C h a p t e r 7 : T h e I n t e r n e t 145 C h a p t e r 7 : T h e I n t e r n e t 145 Variables, Arrays, and Functions $contents String containing the contents of $page How It Works This simple plug-in calls get_file_contents() to load the contents of $page into the string variable $contents. If for any reason the page could not be read in, then FALSE is returned. Otherwise preg_match() is called to extract the contents between the page’s <title> and </title> tags. This is denoted by the (.*) in the expression passed to the function. Of course, some pages may not include a title and this plug-in may fail. So, before returning it checks whether a title has been successfully extracted, and if so, the information needed as a result of calling the function is placed in the second element of the array $matches, and this is then returned. Otherwise, FALSE is returned. How to Use It To extract the title from a page, you can call this plug-in in the following way: $page = "http://news.yahoo.com"; $result = PIPHP_GetTitleFromURL($page); Then, to act on the value returned, use code such as this: if (!$result) echo "The URL could not be accessed"; else echo "The title is: $result"; The Plug-in function PIPHP_GetTitleFromURL($page) { $contents = @file_get_contents($page); if (!$contents) return FALSE; preg_match("/<title>(.*)<\/title>/i", $contents, $matches); if (count($matches)) return $matches[1]; else return FALSE; } Auto Back Links A traffic-building technique that is known to work is to offer automatic back links to sites that link to yours. Using this program, providing that facility is extremely easy and will help you build more traffic with a minimum of extra work. Figure 7-3 shows example output from this plug-in. 43 . containing all the links found at $url $missing Array containing any links passed in $links that are not present in $results $failed Integer counter that indexes into $missing, incrementing on. match $link String containing the current link being processed, as extracted from $links PIPHP_GetLinksFromURL() Plug- in 22: This function returns all the links at a given URL. PIPHP_RelToAbsURL() Plug- in. a link to http://doesnotexist.com/ index.html is not found on the www.pluginphp.com index page. About the Plug- in This plug- in accepts the URL of a web page to check, along with a set of links

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

Từ khóa liên quan

Mục lục

  • Contents

  • Acknowledgments

  • Introduction

  • 1 Building a Development Server

    • Windows XP, Windows Vista, and Windows 7

      • Reinstalling Zend Server CE

      • Upgrading Zend Server CE

      • Windows Security Alerts

      • After Installation

      • Uninstalling

      • Document Root

      • Ubuntu and Debian Linux

        • Uninstalling

        • After Installation

        • Document Root

        • Fedora, RHEL, and CentOS Linux

          • Installing MySQL

          • Uninstalling

          • Document Root

          • Other Versions of Linux

            • Installing MySQL

            • Uninstalling

            • Document Root

            • Mac OS X 10.4 Plus on Intel Chips

              • Document Root

              • Uninstalling

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

Tài liệu liên quan