PHP — Part I

Web-Based Services (INFOSYS 290-3)

Erik Wilde, UC Berkeley iSchool
Wednesday, September 20, 2006
Creative Commons License

This work is licensed under a Creative Commons
Attribution-NonCommercial-ShareAlike 2.5 License.

Abstract

PHP is a technology which can be used on the server side to process HTTP requests. PHP can access other web-based services by accessing them through various interfaces; and it can also be used to implement Web-based services by returning XML instead of HTML. This means that PHP can be used as a simple platform for Web-based services, both for the client and for the server side.

Outline (What's Next?)

  1. What's Next? [3]
  2. PHP Basics [4]
  3. PHP Request Handling [4]
  4. PHP as Glue [3]
  5. Conclusions [1]

My Concrete Plan

  1. HTTP Content Negotiation for Language Selection
  2. Designing an HTML form for a Web-based service
  3. Using an HTTP Web Service
    • turn your form into a functional bibliography query interface
  4. Access Amazon and retrieve Book Prices
    • combine different Web-based services and create a new service
  5. Create a Web Service
    • turn your HTML front-end into an XML front-end (what's your schema?)

My Bigger Picture

Your Expectations?

Outline (PHP Basics)

  1. What's Next? [3]
  2. PHP Basics [4]
  3. PHP Request Handling [4]
  4. PHP as Glue [3]
  5. Conclusions [1]

Server-Side Technologies

PHP Processing

PHP Versions

PHP in Action

<?php phpinfo(); ?>

Outline (PHP Request Handling)

  1. What's Next? [3]
  2. PHP Basics [4]
  3. PHP Request Handling [4]
  4. PHP as Glue [3]
  5. Conclusions [1]

Request-Based Processing

Looking at the Request

<html>
 <head>
  <title>Browser Test</title>
 </head>
 <body>
  <p><?php
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/5') === false ) {
 echo("That's just a Web browser");
} else {
 echo("That's a nice Web browser"); } ?>
 </p>
 </body>
</html>

Looking at the Headers

<html>
 <head>
  <title>Get Request Headers</title>
 </head>
 <body>
  <table>
  <?php
foreach ( $_SERVER as $index=>$value ) {
 if ( strpos($index, "HTTP_") === 0 ) {
 echo "<tr><th align='right' valign='top'>" . substr($index,5) . "</th><td>$value</td></tr>"; } } ?>
  </table>
 </body>
</html>

(try shift-reload vs. no-shift-reload)

Looking at the Server

<html>
 <head>
  <title>Get Request Headers</title>
 </head>
 <body>
  <table>
  <?php
foreach ( $_SERVER as $index=>$value ) {
 echo "<tr><th align='right' valign='top'>$index</th><td>$value</td></tr>"; } ?>
  </table>
 </body>
</html>

(try adding a query string to the script's URI)

Outline (PHP as Glue)

  1. What's Next? [3]
  2. PHP Basics [4]
  3. PHP Request Handling [4]
  4. PHP as Glue [3]
  5. Conclusions [1]

PHP ↔ System

<pre>
<?php system("ls -lasg"); ?>
</pre>
<pre>
<?php system("ls -" . $_SERVER['QUERY_STRING']); ?>
</pre>

PHP ↔ Applications

Simple Service Chain

<?php
include("HttpClient.class.php");
$pageContent = HttpClient::quickGet('http://dret.net/lectures/services-fall06/');
$highlightedContent = str_replace($_SERVER['QUERY_STRING'], "<span style='background-color : yellow'>" . $_SERVER['QUERY_STRING'] . "</span>", $pageContent);
echo $highlightedContent;
?>

Outline (Conclusions)

  1. What's Next? [3]
  2. PHP Basics [4]
  3. PHP Request Handling [4]
  4. PHP as Glue [3]
  5. Conclusions [1]

Simple Server-Side Scripting