|
aohavkbs, http://www.tomshw.it/forum/members/buy-glucophage.html glucophage conveniente, tndgogaw, http://www.tomshw.it/forum/members/comprar-zyprexa.html zyprexa conveniente, vtyyhpgn, http://www.tomshw.it/forum/members/comprar-desyrel.html desyrel conveniente, fxjgwxxe, http://www.tomshw.it/forum/members/comprar-cytotec.html cytotec prezzo ridotto, sazxpbxj, |
Assignment2MikeContinuous Google Online version: http://ernie.art.yale.edu/~michaelgallagher/Project_2/continuousGoogle_11.php Source code: <html> <head> <link rel="stylesheet" type="text/css" href="mike.css" /> <title>Surfin' Safari</title> <script language="JavaScript"> <!-- function searchPopup(term, index, formRef) { // Construct the request URL which will be loaded in the new window url = "mike.php?term=" + term + "&index=" + index; // Construct a unique name for the new window (which is only used internally, it's never displayed) windowName = term + index; // If this function was called directly from a search form, set the search form's target window if (formRef) formRef.target = windowName; // Decide where to put the popup window winWidth = 500; winHeight = 250; winLeft = Math.round(Math.random() * (screen.width - winWidth - 50)); winTop = Math.round(Math.random() * (screen.height - winHeight - 100)); // Consturct the window features string // See http://www.javascripter.net/faq/openinga.htm for description of the JavaScript window.open command and its options windowFeatures = "left=" + winLeft + ", top=" + winTop + ", width=" + winWidth + ", height=" + winHeight + ", toolbar=1, resizable=1"; // Load the request in a new popup window window.open(url, windowName, windowFeatures); } //--> </script> </head> <body> <?php // ----------------------------------- SET-UP TASKS ----------------------------------- // This is my license key $googleKey = '7EV+OalQFHIGdrEgwfVbl0yhcNgnErbz'; // What should we search for if (isset($_REQUEST["term"])) $term = trim($_REQUEST["term"]); // What should the startIndex be if (isset($_REQUEST["index"])) $index = intval($_REQUEST["index"]); else $index = 0; if ($index < 0) $index = 0; // ---------------- DO THIS IF A SEARCH TERM WAS PASSED AS AN ARGUMENT ---------------- if (isset($term)) { // ----- MAKE THE SOAP CLIENT $client = new SoapClient("http://api.google.com/GoogleSearch.wsdl"); // ----- MAKE A REQUEST TO GOOGLE (using remote method invocation) // Do the request repeatedly if it returns an error, until it comes back with no error // Some days, Google seems to return a lot of "500 Internal Server Error"'s, see this thread: // http://groups.google.com/group/google.public.web-apis/browse_thread/thread/3dcd39aef451775d/f0556ba48fc673b4?q=http+error&rnum=6 do { $error = false; try { $response = $client->doGoogleSearch( $googleKey, // the key $term, // the search query $index, // the point in the search results where we want to start 1, // the number of search results (max is 10) false, // don't filter out similar results "", // no restricts false, // no SafeSearch filter "", // no language restricts "", // not used "" // not used ); } catch (Exception $e) { $error = true; echo '<p3>Exception... </p3>' . $e->getMessage() . "<br>\n"; ob_flush(); flush(); } } while ($error); // In PHP's special "try...catch" structure (which is similar to Java's), // the code within the "try" block is always executed. But the code within the // "catch" block is only executed, if an error occurs while executing the code in // the "try" block. Note that try...catch is used for "runtime errors" only-- not // errors in your code, but errors that can't be predicted, such as a problem with // a server you're trying to access. // // Info about the error (technically called an "exception") is passed as a // parameter to the "catch" block. So you should use the code in the "catch" block, // to respond in some way to the potential error. // // In this case all we really do in the "catch" block is set a variable to true; // then we repeat the whole thing if the variable is true, i.e. if there was an // error, using do...while. // ----- UNDERSTAND THE RESPONSE FROM GOOGLE AND DISPLAY THE RESULTS // First check to see if there were any results if (($response->estimatedTotalResultsCount > $index) and (count($response->resultElements) > 0)) { // For convenience, load the resultElements node of the SOAP response into a variable $resultsArray = $response->resultElements; // Go through the array for($resultNum = 0; $resultNum < count($resultsArray); $resultNum++) { // Assign this result to a variable for convenience $thisResult = $resultsArray[$resultNum]; // Display a link to the result website $first_word = $thisResult->title; $first_word = explode(' ',$first_word); //echo '<h1><span class="highlight_5">' . " " . '</span></h1>'; for ($i = 0; $i <= count($first_word); $i++) { echo '<p1><span class="highlight_1"><a href="#" onClick="searchPopup(\'' . $first_word[$i] . '\', 1);">'; // Start with the opening <a href> tag... //echo '<a href="' . $thisResult->URL . '">'; // If a title is available, show it as the thing to click on if ($thisResult->title != "") { // First remove any HTML formatting that Google has inserted into the title //echo strip_tags($thisResult->title) . '</a> </span></p1>'; echo strip_tags($first_word[$i]) . '</a> </span></p1>'; } // If there is no title, show the URL instead as the thing to click on else { // First replace "http://" with "" so it displays nicer echo str_replace("http://", "", $thisResult->URL); } // Finish the link with the closing </a> tag echo "</a>"; } echo '<p1><span class="highlight_2"> </span></p1>'; // So one of these things has been output to the browser: // <a href="http://yale.edu">Yale University</a> // <a href="http://yale.edu">yale.edu</a> // Display a snippet if available if ($thisResult->snippet != "") { // First remove any HTML formatting that Google has inserted into the snippet echo '<p2><span class="highlight_3"> ' . strip_tags($thisResult->snippet) . ' </span></p2>'; echo '<p2><span class="highlight_2"> </span></p2>'; } //display URL echo '<p3><span class="highlight_4"> '; echo str_replace("http://", "", $thisResult->URL); echo ' </span><span class="highlight_2"> </span></p3>'; // Display a header with info echo '<p3><span class="highlight_4"> '; echo "“"; // HTML character entity for left curly quote echo $term; echo "”"; // HTML character entity for right curly quote echo " results "; echo '<p1><span class="highlight_1">' . $response->startIndex . '</span></p1>'; echo " of about "; echo number_format($response->estimatedTotalResultsCount); // number_format() is just for formatting, it displays a comma between every 3 digits echo "</span></p3>"; } // ----- AFTER RESULTS ARE DISPLAYED, REQUEST MORE RESULTS // Because we want the additional results in a new window, we do it by loading this script again into a new window // Send a JavaScript command for the browser to request this PHP program again in a new a popup window, with a different startIndex echo "\n\n"; echo '<script language="JavaScript">' . "\n"; echo '<!--' . "\n"; // Add one to the startIndex for the search we want done in the new window $index++; // For safety, the search term needs to be URL-encoded (e.g a space becomes "+", quote becomes "%22") $term = urlencode($term); // Surround our JavaScript command with the JavaScript setTimeout command so that it's done after a delay $command = "searchPopup(\"$term\", $index);"; $delay = 3000; echo "setTimeout('$command', $delay);" . "\n"; echo '//-->' . "\n"; echo '</script>' . "\n"; } // ----- DISPLAY A MESSAGE IF THERE WERE NO RESULTS else { echo "<h1>No results.</h1>\n"; } } // --------------- DO THIS IF NO SEARCH TERM WAS PASSED AS AN ARGUMENT ---------------- else { // ----- DISPLAY A SEARCH FORM // This form's action is a little unusual because the response needs to load // into a custom popup window rather than the same window as the form. // So we use a JavaScript "onSubmit" handler instead of an HTML "action" // attribute. // Also, to work correctly, the form's "target" attribute needs to be set to // the internal name of the popup window. But this also needs needs to be // done by JavaScript, because the window name has to be computed // dynamically so as to be unique for every search (otherwise windows would // be re-used). So we pass a reference to the form to the searchPopup // function, which can then set the form's target to be the same name as // the popup window the function is creating. // But sometimes the searchPopup function is called by the search results // display (to load the next result), not by the form submit (to load the // first result). In that case, the function still works even when no form // reference is passed. ?> <form name="searchForm" onSubmit="searchPopup(escape(String(this.term.value)), 0, this);"> <p1><span class="highlight_6">Welcome to the Infinite Google. </span><br> <span class="highlight_5">Where do you want to explore? </span><br> <span class="highlight_1">What would you like to search for? </span></p1><br> <input type="text" name="term"> <input type="submit" value="Submit the search"> </form> <?php } ?> </body> </html> Style sheet: body {background-color: white} span.highlight_1 {background-color: #FFBC00} span.highlight_2 {background-color: #666666} span.highlight_3 {background-color: #CECECE} span.highlight_4 {background-color: #E1E1E1} span.highlight_5 {background-color: #FFA201} span.highlight_6 {background-color: #FF8401} p1 {font-family: andale mono; font-size: 225%; color: black; line-height: 112%} p2 {font-family: andale mono; font-size: 120%; color: black; line-height: 110%} p3 {font-family: andale mono; font-size: 75%; color: black; line-height: 110%} p4 {font-family: andale mono; font-size: 100%; color: #black; line-height: 112%} a:link {color: #000000; text-decoration: none} a:visited {background-color: #FFA201; text-decoration: none} a:hover {background-color: #FFA201; text-decoration: none} a:active {color: #0000FF; text-decoration: none} |