1 / 15

Contacting other servers

Learn how to contact other servers and retrieve data using server-to-server connection, AJAX, and script tags. Practice creating a MySQL database and table, inserting rows, and outputting them to a browser.

ernestoc
Download Presentation

Contacting other servers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Contacting other servers http://www.flickr.com/photos/torkildr/3462607995/

  2. Activity #1 • Practice what we covered last week. • Gather with your team • Use one team member's laptop; others look on • Create a MySQL database • Create a table • Insert some rows into the table • 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 Remoteserver Browser Server Click link ortype input, etc Send params Compute Send/receive data fromanother 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 • you hardcode the "to" address as in this example • you hardcode the body as in this example • 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 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 • Sending a text messageis as simple as sendinga 140-character email. • The recipient's usernameis 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

  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 "<ul>"; for ($i = 0; $i < $nitems; $i++) { $title = $items[$i]->title; $link = $items[$i]->link; echo "<li><a href='" . htmlspecialchars($link); echo "'>" . htmlspecialchars($title) . "</a>"; } echo "</ul>"; // var_dump($xml); ?> /* simplexml_load_file returns an object. Object properties are dereferenced with the -> syntax. */

  11. Server-to-server vs AJAX vs <script> 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") • <script src="http://anotherserver.com/a.js"> • 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 <SCRIPT> • <script src="http://anotherserver.com/blahblah.php?city=Corvallis"> • When you need to contact a server on another domain, and the other server does provide a JS file that you can include with <SCRIPT> • AJAX • All other situations

  13. Example: Showing a Google mapNo need for server-to-server connection! <!DOCTYPE html> <html> <head> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA3SIW0qcGWJXuchhohp-NNNeTQdo_KUuM&sensor=false"></script> <script> function init() { vargeocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': "Portland, OR"}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { varlatlng = results[0].geometry.location; varconfig = { 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); } }); } </script></head> <body onload="init()"> <div id="mapdiv" style="width: 400px; height: 400px;"></div></body></html>

  14. Example: Showing Facebook postsNo need for server-to-server connection! <iframesrc="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2F%3Fref%3Dhome%5C%23%21%2FRickAstley&amp;width=500&amp;colorscheme=light&amp;connections=10&amp;stream=true&amp;header=false&amp;height=400" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:500px; height:400px;" allowTransparency="true"></iframe>

  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 <SCRIPT> • Then go for it • Else • Fall back on server-to-server connection

More Related