Hi All,
I have a google-api nusoap search window that works great, but displays the results over the current page, which is unreadable.
What I am trying to accomplish is to send the results to a new page called search.php.
This page (and the page where you enter the search paramaters) have includes for the headers, scripts and footers, so it is not easy to display the code here.
So here is the code for the google-search:
<?php
////////////////////////////////////////////////////////////
// This is example code of how to query the Google API using
// Web Services, SOAP, and PHP.
//
// Author: Geoff Peters, January 6th 2004.
// Updated by Dan Karran, 10th March 2005 to utilise nuSOAP instead of PEAR.
// put your developer's key here:
$key = 'Put your key here ( I removed this for security reasons)';
include ('nusoap.php');
$soapclient = new soapclient('http://api.google.com/search/beta2');
$soapoptions = 'urn:GoogleSearch';
// Ensure there is a start value
if (!$start) {
$start = 0;
} else {
$start = intval($start-1);
}
////////////////////////////////////////////////////////////
// Calls the Google API and retrieves the search results in $ret
//
function do_search( $q, $type, $key, $start, &$ret )
{
global $soapclient;
global $soapoptions;
// Note that we pass in an array of parameters into the Google search.
// The parameters array has to be passed by reference.
// The parameters are well documented in the developer's kit on the
// Google site http://www.google.com/apis
// limit searches to this server
$sitequery = "$q site:{$_SERVER['SERVER_NAME']} $restrict";
$params = array(
'key' => $key,
'q' => $sitequery,
'start' => $start,
'maxResults' => 10,
'filter' => false,
'restrict' => '',
'safeSearch' => false,
'lr' => '',
'ie' => '',
'oe' => ''
);
// Here's where we actually call Google using SOAP.
// doGoogleSearch is the name of the remote procedure call.
$ret = $soapclient->call('doGoogleSearch', $params, $soapoptions);
$err = $soapclient->getError();
if ($err)
{
print("<br>An error occurred!<br>");
print(" Error: $err<br>\n");
return false;
}
return true;
}
////////////////////////////////////////////////
// Does Google search with retry.
// Retry is useful because sometimes the connection will
// fail for some reason but will succeed when retried.
function search( $q, $type, $key, $start, &$ret )
{
$result = false;
$max_retries = 5;
$retry_count = 0;
while( !$result && $retry_count < $max_retries )
{
$result = do_search( $q, $type, $key, $start, $ret );
if( !$result )
{
print( "Attempt $retry_count failed.<br>\n");
}
$retry_count++;
}
if( !$result )
{
print("<br>Sorry, connection to Google failed after retrying several times.<br>\n");
}
return $result;
}
////////////////////////////////////////////////////////////
// Calls the Google API and retrieves the suggested spelling correction
//
function do_spell( $q, $key, &$spell )
{
global $soapclient;
global $soapoptions;
$params = array(
'key' => $key,
'phrase' => $q,
);
$spell = $soapclient->call('doSpellingSuggestion', $params, $soapoptions);
$err = $soapclient->getError();
if ($err)
{
print("<br>An error occurred!<br>");
print(" Error: $err<br>\n");
return false;
}
return true;
}
//////////////////////////////////////////////////////////
// The main part of this script
if( $q != "" )
{
// remove the slashes that are automatically added by PHP before each quotation mark
$q = stripslashes($q);
if( do_search( $q, $type, $key, $start, $ret ) )
{
$count = $ret['estimatedTotalResultsCount']; // total number of results
$secs = round($ret['searchTime'],2); // time taken to search (in seconds)
$min = $ret['startIndex']; // first record returned
$max = $ret['endIndex']; // last record returned
if ($max) {
// Truncate query for display
if (strlen($q) > 36) {
$short_q = substr($q,0,33)."...";
} else {
$short_q = $q;
}
// print header with search box and details of search results
print "<form action=\"../includes/search2.php\" method=\"GET\"><table class=\"search_top\" width=100%><tr><td nowrap style=\"text-align:left;\">";
print "<input type=\"text\" name=\"q\" target=\"_blank\" size=\"30\" value=\"$q\">\n";
print "<input type=\"submit\" value=\"search\">\n";
print "</td><td nowrap></td></tr></table></form>";
// list results
foreach($ret['resultElements'] as $result) {
// Make URLs more friendly for user by removing http:// and highlighting where necessary
$friendly_URL = $result['URL'];
$friendly_URL = str_replace("http://","",$friendly_URL);
$friendly_URL = str_replace("$q","<b>$q</b>",$friendly_URL);
print "<p class=\"search_result\">";
if (!$title = $result['title']) {
$title = $result['URL'];
print "<a href=\"".$result['URL']."\">".$friendly_URL."</a>\n";
} else {
print "<a href=\"".$result['URL']."\">".$title."</a><br/>\n";
if ($result['snippet']) {
print $result['snippet']."<br/>\n";
}
print "<span class=\"search_url\">$friendly_URL</a>";
}
print "</p>\n\n";
}
} else {
print "<p>Sorry, no results were found on this site for <b>$q</b>. ";
do_spell($q, $key, $spell);
if ($spell[0]) {
print "Did you mean <b><a href=\"../search/?q=".$spell."\">".$spell."</a></b>? ";
}
print "Occasionally no results will be returned when there are problems with the link between this site and Google. If you believe the information is on the site but it tells you that it's not, please try again.</p>";
}
}
}
// Show forward/backward navigation if there are results
if ($count) {
print "<div class=\"search_bottom\">";
if ($min>1) {
if ($min >10) {
$prevpage = $min-10;
} else {
$prevpage = 1;
}
print " <b><a href=\"../search/?q=".$q."&start=".$prevpage."\">previous page</a></b> | ";
} else {
print " previous page | ";
}
if ($count>$max) {
$nextpage = $max+1;
print " <b><a href=\"../search/?q=".$q."&start=".$nextpage."\">next page</a></b> ";
} else {
print " next page ";
}
print "</div>";
}
print "<form action=\"http://www.intheclassroom.org/noFrames/includes/search2.php\" method=\"GET\" class=\"search_main\" target=\"_blank\">";
print "<input type=\"text\" name=\"q\" size=\"25\" value=\"$q\"> <input type=\"submit\" value=\"search\">";
print "</form>";
?>
And here is the search.php page (which is where I want to display the results from the above code):
<html>
<head>
<title>In The Classroom Media</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../font.css" rel="stylesheet" type="text/css">
<? include('http://www.intheclassroom.org/noFrames/includes/header_Scripts.shtm'); ?>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="765" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="760">
<? include ("http://www.intheclassroom.org/noFrames/includes/header_test2.php") ?>
</td>
</tr>
<tr>
<td class="font"> Display the Results here.
<?
$results = $_GET['q'];
echo $results;
?>
</tr>
</td>
</tr>
<tr><td>
<? include ('http://www.intheclassroom.org/noFrames/includes/footer.php'); ?>
</td></tr>
</table>
</body>
</html>
I am not sure, but I think I need to do a:
$results = $_GET['q'];
echo $results;
But that only gives me what I entered into the search window, not the results
Thanks for the help,
Don