Recent Changes - Search:

Network Programming

This website demonstrates using wikis as teaching and learning tool.

The course instructor is happy to share the teaching materials here with those who find it readable.

Access Web Services with PHP


Activity One

  • Study the following PHP script. Answer the questions that follow. You can discuss the questions with your classmates.
    <?php

    $api_service_url = "http://search.yahooapis.com/SiteExplorerService/V1/inlinkData";
    $apiid = "pQzlchXV34GhgyFFcNhE8ijvqemS0pw4G3VIf1fLyspIx.9Cg5cRIU1ESXNHD9c6BXXZ";
    $query  = "http://www.stevenchoy.com";
    $entire_site  = "";   // "1" to provide results for the entire site
    $omit_inlinks = "domain";
    $linksperrequest = 10;   // 100 is max value
    $startposition = 1;

    $request_url = sprintf("%s?appid=%s&query=%s&entire_site=%s&omit_inlinks=%s&output=php", $api_service_url, $apiid, urlencode($query), $entire_site, $omit_inlinks);

    $currentpos = 0;
    while ($currentpos++ >= 0) {

       // Formating API request url
       $requrl = sprintf("%s&start=%s&results=%s", $request_url, ($currentpos-1)*$linksperrequest+$startposition, $linksperrequest);

       // Handle errors
       if (($content = file_get_contents($requrl)) === FALSE ) {
       echo "HTTP error: $requrl";
       exit;
       }

       // Unserialize data from response
       $data = unserialize($content);

       if (!array_key_exists("ResultSet", $data)) {
       echo "Error: Bad response from server";
       exit;
       }

       // Extracting each link from array
       for ($i=0; $i<sizeof($data["ResultSet"]["Result"]); $i++) {
          $url = $data["ResultSet"]["Result"][$i]["Url"];
          $title = $data["ResultSet"]["Result"][$i]["Title"];

          echo '<p><a href="'.$url.'">'.$title.'</a></p>';


       }

       // End loop if no more results
       if (sizeof($data["ResultSet"]["Result"]) < $linksperrequest) break;
    }

    ?>
(The API key shown in the PHP script is not a real one. Please obtain and use your own API key if you want to test the program in your server.)

Questions for Activity One

  • Find the webpage that has the details about the API used in the script.
  • State the request URL.
  • State all the possible request parameters.
  • State only the required request parameters.
  • What types of Web Services (SOAP, REST, or XML-RPC) the script accesss?
  • Can you test the Web Service API without the script? If yes, explain how. If not, explain.

Activity Two

  • Run the PHP script with a Web server that supports PHP.
  • Test the Web Service API with different queries.

Activity Three

  • Test the Web Service API only with your browser.
  • Write down the general format of the XML in the response.

Submission

  • Please submit your work to Steven by email.
  • Email: sochoy@ouhk.edu.hk
  • Email title: NP Tutorial 15 Submission -SXXXXXXX

Edit - History - Print - Recent Changes - Search
Page last modified on February 02, 2010, at 10:34 AM