CONTACTING OTHER SERVERS.

15
://www.flickr.com/photos/torkildr/3462607995/ CONTACTING OTHER SERVERS

description

Overview of contacting other servers Sometimes your web server needs some help – Sending an – Sending a text message – Retrieving data XML files, JSON, even HTML Often, this can be accomplished using a server-to-server connection

Transcript of CONTACTING OTHER SERVERS.

Page 1: CONTACTING OTHER SERVERS.

http://www.flickr.com/photos/torkildr/3462607995/

CONTACTING OTHER SERVERS

Page 2: CONTACTING OTHER SERVERS.

Activity #1

• Practice what we covered last week. 1. Gather with your team2. Use one team member's laptop; others look on3. Create a MySQL database4. Create a table5. Insert some rows into the table6. Output the rows to a browser

Page 3: CONTACTING OTHER SERVERS.

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

Page 4: CONTACTING OTHER SERVERS.

Example of server-to-server connection

Browser Server Remoteserver

Click link ortype input, etc Send params

Compute

Send/receive data fromanother server

Send back page

Page 5: CONTACTING OTHER SERVERS.

Example: Sending an email

<?php $to = "[email protected]"; $subj = "you are a cool dude"; $body = "I sure like reading your blog.\r\n\r\nSigned, your bud"; $hdr = "From: [email protected]\r\nX-Mailer: php"; if (mail($to, $subj, $body, $hdr)) echo("It is a good day"); else echo("It is a sad day"); ?>

Page 6: CONTACTING OTHER SERVERS.

Security warning

• Note: Spammers will use your web form to send spam, unless if a) you hardcode the "to" address as in this exampleb) you hardcode the body as in this examplec) 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.

Page 7: CONTACTING OTHER SERVERS.

Example: Sending a text message

• Sending a text messageis as simple as sendinga 140-character email.

• The recipient's usernameis the recipient's phone #

• Example:[email protected] 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.comT-Mobile - tmomail.netAT&T - mobile.att.net

Sprint - messaging.sprintpcs.com

Page 8: CONTACTING OTHER SERVERS.

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() */

Page 9: CONTACTING OTHER SERVERS.

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. */

Page 10: CONTACTING OTHER SERVERS.

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. */

Page 11: CONTACTING OTHER SERVERS.

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

Page 12: CONTACTING OTHER SERVERS.

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

Page 13: CONTACTING OTHER SERVERS.

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() { 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); } }); }</script></head><body onload="init()"> <div id="mapdiv" style="width: 400px; height: 400px;"></div></body></html>

Page 14: CONTACTING OTHER SERVERS.

Example: Showing Facebook postsNo need for server-to-server connection!

<iframe src="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>

Page 15: CONTACTING OTHER SERVERS.

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