CONTACTING OTHER SERVERS http www flickr comphotostorkildr3462607995 Activity

  • Slides: 15
Download presentation
CONTACTING OTHER SERVERS http: //www. flickr. com/photos/torkildr/3462607995/

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

Activity #1 • Practice what we covered last week. 1. 2. 3. 4. 5.

Activity #1 • Practice what we covered last week. 1. 2. 3. 4. 5. 6. Gather with your team Use one team member's laptop; others look on Create a My. SQL database Create a table Insert some rows into the table Output the rows to a browser

Overview of contacting other servers • Sometimes your web server needs some help –

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

Example of server-to-server connection Browser Remote server Server Click link or type input, etc

Example of server-to-server connection Browser Remote server Server Click link or type input, etc Send params Compute Send back page Send/receive data from another server

Example: Sending an email <? php $to = "cscaffid@gmail. com"; $subj = "you are

Example: Sending an email <? php $to = "cscaffid@gmail. com"; $subj = "you are a cool dude"; $body = "I sure like reading your blog. rn. Signed, your bud"; $hdr = "From: scaffidc@onid. orst. edurn. X-Mailer: php"; if (mail($to, $subj, $body, $hdr)) echo("It is a good day"); else echo("It is a sad day"); ? >

Security warning • Note: Spammers will use your web form to send spam, unless

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.

Example: Sending a text message • Sending a text message is as simple as

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 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 would be a Verizon user • Sadly, you need to know the recipient's carrier (see inset box above) to figure out the domain

Example: Doing a GET to a server <? php $html = file_get_contents("http: //search. oregonstate.

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

Example: Doing a POST to a server <? php $data = array('q' => 'test');

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 )); $stream. Context = stream_context_create($ctxinfo); $file = fopen("http: //search. oregonstate. edu/", 'rb', false, $stream. Context); 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. */

Example: Retrieving an RSS feed <? php $url = "http: //rss. cnn. com/rss/cnn_tech. rss";

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

Server-to-server vs AJAX vs <script> tag • Server-to-server – Your PHP initiates a connection

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

When to use… • Server-to-server – When you need to contact a server on

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

Example: Showing a Google map No need for server-to-server connection! <!DOCTYPE html> <head> <script

Example: Showing a Google map No need for server-to-server connection! <!DOCTYPE html> <head> <script src="https: //maps. googleapis. com/maps/api/js? key=AIza. Sy. A 3 SIW 0 qc. GWJXuchhohp-NNNe. TQdo_KUu. M&sensor=false"></script> <script> function init() { var geocoder = new google. maps. Geocoder(); geocoder. geocode( { 'address': "Portland, OR"}, function(results, status) { if (status == google. maps. Geocoder. Status. OK) { var latlng = results[0]. geometry. location; var config = { zoom: 8, center: latlng, map. Type. Id: google. maps. Map. Type. Id. ROADMAP }; map = new google. maps. Map(document. get. Element. By. Id("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: 400 px; height: 400 px; "></div></body></html>

Example: Showing Facebook posts No need for server-to-server connection! <iframe src='data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220%200%20415%20289%22%3E%3C/svg%3E' data-src="http: //www. facebook. com/plugins/likebox.

Example: Showing Facebook posts No need for server-to-server connection! <iframe src="http: //www. facebook. com/plugins/likebox. ph p? href=http%3 A%2 F%2 Fwww. facebook. com%2 F%3 Fref%3 Dhome%5 C%23%21%2 FRick. Astley& width =500& colorscheme=light& connections=1 0& stream=true& header=false& heig ht=400" scrolling="no" frameborder="0" style="border: none; overflow: hidden; width: 500 px; height: 400 px; " allow. Transparency="true"></iframe>

Summary: Contacting other servers • When you need data from a server – If

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