170 likes | 314 Views
Web Scripting [PHP] CIS166AE. Wednesdays 6:00pm – 9:50pm Rob Loy. Agenda. Rest of the year Web services overview RSS news Server side web service In-class: Own web service. Rest of the year. Nov 28 – Web Service (SOAP) Dec 5 – Object Oriented Programming & PHP Applications
E N D
Web Scripting [PHP]CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy
Agenda • Rest of the year • Web services overview • RSS news • Server side web service • In-class: Own web service
Rest of the year • Nov 28 – Web Service (SOAP) • Dec 5 – Object Oriented Programming & PHP Applications • Dec 12 – OPTIONAL – Final Project Lab • Dec 14 – Final Projects Due
Web Services Model WEBSITE DB WEB SERVICE USER
IMPORTANT NOTE RSS is a standard for news across all platforms. This allows “readers” to display news the same from various sources. http://en.wikipedia.org/wiki/RSS
RSS News http://rss.cnn.com/rss/cnn_world.rss
RSS/XML <item> <title>Yemen's Saleh vows to step down as president</title> <link>http://rss.cnn.com/~r/rss/cnn_world/~3/6o9HGSh-ZcA/index.html</link> <description>Yemen&apos;s embattled President Ali Abdullah Saleh will leave office &quot;within 90 days&quot; of an agreement with the regional Gulf Cooperation Council, he said in an interview</description> <pubDate>Tue, 15 Nov 2011 05:18:48 EST</pubDate> </item>
Consuming RSS with PHP(1) $ch = curl_init("http://sports.espn.go.com/espn/rss/nfl/news"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, 0); $data = curl_exec($ch); curl_close($ch); $doc = new SimpleXmlElement($data, LIBXML_NOCDATA); parseRSS($doc); http://ditio.net/2008/06/19/using-php-curl-to-read-rss-feed-xml/
Consuming RSS with PHP(2) function parseRSS($xml) { echo "<h2>".$xml->channel->title."</h2>>\n"; $cnt = count($xml->channel->item); for($i=0; $i<$cnt; $i++) { $url = $xml->channel->item[$i]->link; $title = $xml->channel->item[$i]->title; $desc = $xml->channel->item[$i]->description; echo “<p><a href=\””.$url.”\”>”.$title.”</a>”; echo $desc.”</p>\n”; } } http://ditio.net/2008/06/19/using-php-curl-to-read-rss-feed-xml/
RSS Options #2 • $xml = "http://www.nytimes.com/services/xml/rss/nyt/business-computing.xml"; • $xmldocument = new DOMDocument(); • $xmldocument->load($xml); • $source = $xmldocument-> • getElementsByTagName('channel')-> • item(0); • $sourcetitle = $source-> • getElementsByTagName('title')-> • item(0)-> • childNodes-> • item(0)-> • nodeValue; • echo $sourcetitle; • $story = $xmldocument-> • getElementsByTagName('item'); • for ($x = 0; $x < 5; $x++) • { • $title = $story-> • item($x)-> • getElementsByTagName('title')-> • item(0)-> • childNodes-> • item(0)-> • nodeValue; • $link = $story-> • item($x)-> • getElementsByTagName('link')-> • item(0)-> • childNodes-> • item(0)-> • nodeValue; • $description = $story-> • item($x)-> • getElementsByTagName('description')-> • item(0)-> • childNodes-> • item(0)-> • nodeValue; • $date = $story-> • item($x)-> • getElementsByTagName('pubDate')-> • item(0)-> • childNodes-> • item(0)-> • nodeValue; • echo "<p><a href=\"" . $link. "\">". $title . "</a></p>\n"; • echo "<p style=\"font-size:12px;\">" . $description . "</p>\n"; • echo "<p>" . $date . "</p>\n"; • echo "<hr />"; • }
PHP Client Side WS // Pull in the NuSOAP code require_once('../lib/nusoap.php'); // Create the client instance $client = new nusoap_client('http://www.robloy.com/soap/server.php'); // Call the SOAP method $result = $client->call('hello', array('name' => 'Scott', 'age'=> '12')); // Display the result echo '<h2>Result</h2><pre>'; print_r($result); echo '</pre>'; http://www.codedeveloper.net/2010/05/nusoap-soap-php-web-services-tutorial-part-one/
PHP Server Side WS // Pull in the NuSOAP code require_once('../lib/nusoap.php'); // Create the server instance $server = new soap_server; // Register the method to expose $server->register('hello'); // Define the method as a PHP function function hello($name, $age) { return 'Hola, ' . $name . ', you have $'. $age;} // Use the request to (try to) invoke the service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA); http://www.codedeveloper.net/2010/05/nusoap-soap-php-web-services-tutorial-part-one/
Weather Web Service • <?php • $url ="http://w1.weather.gov/xml/current_obs/KSDL.xml"; • $xml = simplexml_load_file($url); • echo "<h1>Phoenix Weather</h1>"; • echo $xml->weather . "<br />"; • echo $xml->temperature_string . "<br />"; • echo $xml->wind-speed . "<br />"; • echo $xml->relative_humidity. "%<br />"; • ?>
IMPORTANT NOTE In PHP you can build both the Client and Server piece of a Web Service. Any other language/site can access a web service using HTTP. For example an .NET application can call a PHP web service.
Lab • Make an web service that determines how much to leave for a 15% tip (tipcalculator.php) • Make a web page that a user inputs bill amount and calls the tip calculator to print out the bill, tip and total amounts. • Email URL to rob.loy@scottsdalecc.edu by Dec 5 at 6:00pm
Final Project • Create a PHP website using pieces of code and modules we learned in class • Make the site look like a finished product that you can share as a indication of your skills • You will be graded on looks, details and demonstration of php knowledge • Send email to rob.loy@gmail.com with URL to input form file before 6pm on Dec 14.