|
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. |
Tutorial /
Access Web Services with PHPOverview: In this tutorial, you will study how to use PHP to access Site Explorer APIs from Yahoo! Search. ![]() Activity One
<?php
$api_service_url = "http://search.yahooapis.com/SiteExplorerService/V1/inlinkData";
$apiid = "pQzlchXV34GhgyFFcNhE8ijvqemS0pw4G3VIf1fLyspIx.9Cg5cRIU1ESXNHD9c6BXXZ";
$query = "http://www.stepwise.hk";
$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(Q1) Find the webpage that has the details about the API used in the script.
(Q2) State the request URL.
(Q3) State all the possible request parameters.
(Q4) State only the required request parameters.
(Q5) What types of Web Services (SOAP, REST, or XML-RPC) the script accesss?
(Q6) Can you test the Web Service API without the script? If yes, explain how. If not, explain.
Activity Two
Activity Three
(Q7) Write down the general format of the XML in the response.
Submission
|