Program Yahoo! with PHP 5

Take advantage of some of the latest features in PHP to quickly add Yahoo! data to PHP-powered pages.
The recursively named PHP Hypertext Processor language is a popular choice for building dynamic web applications. In fact, Yahoo! itself has made PHP its development platform of choice across the company.

 The PHP platform is continually evolving, and the latest version Version 5 includes a handy XML parser called SimpleXML. As the name implies, it's easy to work with. And as long as the XML that SimpleXML is parsing is fairly simple, it's the perfect tool for getting XML data into objects PHP can easily manipulate.
Yahoo! Search Web Services responses definitely qualify as simple XML, and this hack shows how easy it is to request and parse this data with PHP. You'll need PHP 5 for this hack, but you won't need any external modules.

<?php
// yahoo_search.php
// Accepts a search term and shows the top results.
// Usage: yahoo_search.php?p=<Query>
//
// You can create an AppID, and read the full documentation
// for Yahoo! Web Services at http://developer.yahoo.net/

// Set your unique Yahoo! Application ID
$appID = "insert your app ID";

// Grab the incoming search query, and encode for a URL
$query = $_GET['p'];
$query = urlencode($query);

if ($query == "") { 
 print "usage: yahoo_search.php?p=&lt;Query&gt;"; die;
}

// Construct a Yahoo! Search Query with only required options
$language = "en";
$req_url = "http://api.search.yahoo.com/";
$req_url .= "WebSearchService/V1/webSearch?";
$req_url .= "appid=$appID";
$req_url .= "&query=$query";
$req_url .= "&language=$language";

// Make the request
$yahoo_response = file_get_contents($req_url);

// Parse the XML
$xml = simplexml_load_string($yahoo_response);

// Initialize results counter
$i = 0;
?>
<html>

<body>
<h2>Yahoo! Search Results</h2>
<ol>
<?php
// Loop through the items returned, printing them out
foreach ($xml->Result as $result) {
 $i++;
 $title = $result->Title;
 $summary = $result->Summary;
 $summary = preg_replace("/</i","&lt;",$summary);
 $clickurl = $result->ClickUrl;
 $url = $result->Url;
 print "<li><div style=\"margin-bottom:15px;\">";
 print "<a href=\"$clickurl\">$title</a><br />";
 print "$summary<br />";
 print "<cite>$url</cite></div></li>\n";

}
?>
</ol>
-- Results Powered by Yahoo!
</body>
</html>

This script uses the value of the querystring variable p to build a Yahoo! Web Search request URL and fetches the XML with the file_get_contents() function. Once the script has the XML in the $yahoo_response string, it calls the SimpleXML function simplexml_load_string( ), which parses the XML and makes the data available to PHP as an object. Finally, the script loops through the objects, using print to send the data to the browser.

To run the script, point your web browser to the location of the script on your server and add the querystring variable p:
http://example.com/yahoo_search.php?p=insert word

You can add multiple words by encoding spaces for URLs. For example, here's the search string for "PHP encoding":
http://example.com/yahoo_search.php?p=PHP%20encoding
 
As the results indicate, you can read the official documentation for PHP's 
SimpleXML function at http://www.php.net/simplexml. With this function, working with 
Yahoo! Search Web Services data is much more 
intuitive than with earlier versions of PHP. 



Related Posts:
  • configuring PHP-impact security The primary mechanism for configuring PHP is the php.inifile. As the master file, this provides you with control over all configuration settings. Entries generally take the format:setting= value Be sure to read the comment… Read More
  • PHP-script-Related Variables PHP-script Related Variables PHP automatically creates variables for all the data it receives in an HTTP request. This can include GET data, POST data, cookie data, and environment variables. The variables are e… Read More
  • what is CodeIgniter Helpers? Helpers, as their name implies, help you with specific tasks. Unlike libraries, helpers are not object -oriented but procedural in nature. Each helper contains one or more functions, each focusing on a specific task, with ze… Read More
  • php-Data Types Data Types PHP provides four primitive data types: integers, floating point numbers, strings, and booleans. In addition, there are two compound data types: arrays and objects.  Integers Integers are whole num… Read More
  • codeigniter routes page The  routes.php filelets you remap URI requests to specific controller functions. For example, you may have a controller named sitewith a function named index. The URI for this controller/function combination might be… Read More
  • PHPGTK-application window PHPGTK is an extension to PHP that allows you to create graphical user interface (GUI) applications. Instead of running in a browser, your PHP application runs in its own application window. These applications are clie… Read More
  • Advantages of MySQL and PHP Certain technologies play together better than others. PHP, a simple and powerful scripting language, and MySQL, a solid and reliable database server, make a perfect marriage between two modern technologies for building da… Read More
  • PHP-max_execution_time php.ini Directives Related to the Connection-Handling Functions The following configuration directives can be used to control the behavior of the connection-handling functions. Directive Name Value Type Descri… Read More
  • php.ini Settings for Session Management Before you get started with this chapter, you may have to make a couple of minor changes to your php.ini file so that sessions work correctly. On Windows If you are using a Windows version of PHP, the first thing you… Read More
  • Make a PHP session code  Make a PHP session code session_start int session_start(void) Initializes a session. Returns: Always returns TRUE Description: Initializes a session. If a session ID is sent as a GET or in a cookie and is … Read More
  • PHP-MySQL application Security With these two methods, there’s no longer any need to ever use GET for requests internal to an application. You may still need it for external requests, to other applications and web sites that aren’t coded to look for their… Read More
  • Manage Databases on a Server by php MySQL-related functions. mysql_list_dbs() Used to list the databases on a MySQL server. mysql_num_rows() Returns the number of rows in a result set. mysql_tablename() Despite its name, can extract the name of … Read More
  • HTTP Request Methods-PHP !--#brandmenu { background: none repeat scroll 0 0 #f6f6f6; border-bottom: 2px solid #777; /*clear: left;*/ float: left; margin-top: 3px; padding: 0; position: relative; width: 78px; min-he… Read More
  • How to Enable mod_rewrite at Server mod_rewrite may not be enabled by default in your Apache configuration. To make sure,open the Apache configuration file, named httpd.conf. The full path of the file will be \Program Files\xampp\apache\conf\httpd.conf. Open … Read More
  • top Database management system-PHP PHP Support for Multiple Databases  If you have been playing with PHP for a while, you have most likely noticed its excellent support for connecting to MySQL databases. Most of the PHP books on the market describ… Read More