|
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 PHPActivity One
<?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
Activity Two
Activity Three
Submission
|