Download presentation
Presentation is loading. Please wait.
1
http://www.flickr.com/photos/torkildr/3462607995/ CONTACTING OTHER SERVERS
2
Activity #1 Practice what we covered last week. 1.Gather with your team 2.Use one team member's laptop; others look on 3.Create a MySQL database 4.Create a table 5.Insert some rows into the table 6.Output the rows to a browser
3
Overview of contacting other servers Sometimes your web server needs some help – Sending an email – Sending a text message – Retrieving data XML files, JSON, even HTML Often, this can be accomplished using a server-to-server connection
4
Example of server-to-server connection Browser Server Remote server Click link or type input, etc Send params Compute Send/receive data from another server Send back page
5
Example: Sending an email <?php $to = "cscaffid@gmail.com"; $subj = "you are a cool dude"; $body = "I sure like reading your blog.\r\n\r\nSigned, your bud"; $hdr = "From: scaffidc@onid.orst.edu\r\nX-Mailer: php"; if (mail($to, $subj, $body, $hdr)) echo("It is a good day"); else echo("It is a sad day"); ?>
6
Security warning Note: Spammers will use your web form to send spam, unless if a)you hardcode the "to" address as in this example b)you hardcode the body as in this example c)OR you protect the form behind login or other authentication. Also, this example should be checking for a POST. Don't send an email on a GET as above.
7
Example: Sending a text message Sending a text message is as simple as sending a 140-character email. The recipient's username is the recipient's phone # Example: 5415551212@vtext.com would be a Verizon user Sadly, you need to know the recipient's carrier (see inset box above) to figure out the domain You need to use a different domain depending on which Short Messaging Service (SMS) carrier the recipient has: Verizon - vtext.com T-Mobile - tmomail.net AT&T - mobile.att.net Sprint - messaging.sprintpcs.com
8
Example: Doing a GET to a server <?php $html = file_get_contents("http://search.oregonstate.edu/?q=test"); echo htmlspecialchars($html); ?> /* This is VERY handy for working around the single origin policy of JS, since PHP can access any server (unless your own server admin configures your own server to prevent contacting other servers) */ /* If your URL parameter values contain non-alphanumeric characters, encode them with urlencode() */
9
Example: Doing a POST to a server <?php $data = array('q' => 'test'); // you could have additional parameters $ctxinfo = array('http' => array( 'method' => 'POST', 'content' => $data )); $streamContext = stream_context_create($ctxinfo); $file = fopen("http://search.oregonstate.edu/", 'rb', false, $streamContext); if (!$file) echo "Error on open"; $html = @stream_get_contents($file); if (!$html) echo "Error on read"; echo htmlspecialchars($html); ?> /* This example will URL encode your POST parameters for you. */
10
Example: Retrieving an RSS feed <?php $url = "http://rss.cnn.com/rss/cnn_tech.rss"; $xml = simplexml_load_file($url); // note: doesn't work with https due to our own server's configuration $title = $xml->channel[0]->title; $items = $xml->channel[0]->item; $nitems = count($items); echo " "; for ($i = 0; $i < $nitems; $i++) { $title = $items[$i]->title; $link = $items[$i]->link; echo " <a href='". htmlspecialchars($link); echo "'>". htmlspecialchars($title). " "; } echo " "; // var_dump($xml); ?> /* simplexml_load_file returns an object. Object properties are dereferenced with the -> syntax. */
11
Server-to-server vs AJAX vs tag Server-to-server – Your PHP initiates a connection to remote server – Any remote server can be contacted AJAX (covered later in this course) – Your JS initiates a connection to YOUR server – Only servers in your own (sub-)domain can be contacted ("single origin policy") – The remote JS becomes part of your page – The remote server has to provide the JS
12
When to use… Server-to-server – When you need to contact a server on another domain, and the other server does not provide a JS file you can include with – When you need to contact a server on another domain, and the other server does provide a JS file that you can include with AJAX – All other situations
13
Example: Showing a Google map No need for server-to-server connection! function init() { var geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': "Portland, OR"}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var latlng = results[0].geometry.location; var config = { zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("mapdiv"), config); new google.maps.Marker({map: map, position: latlng}); } else { alert("Error occurred: " + status); } }); }
14
Example: Showing Facebook posts No need for server-to-server connection! <iframe src="http://www.facebook.com/plugins/likebox.ph p?href=http%3A%2F%2Fwww.facebook.com%2F%3 Fref%3Dhome%5C%23%21%2F RickAstley &width =500&colorscheme=light&connections=1 0&stream=true&header=false&heig ht=400" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:500px; height:400px;" allowTransparency="true">
15
Summary: Contacting other servers When you need data from a server – If it's your own server, AJAX is great – Else if the server offers JS you can use in Then go for it – Else Fall back on server-to-server connection
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.