Ajax request for google search results CURL
This code can be used to retrieve Google results for a keyword using ajax.
//This section will be excuted for ajax request and can be placed in seperate file
if(isset($_REQUEST['search']))
{
$ch = curl_init();
// set URL and other appropriate options
$result_cnt=10; //can be changed
$search=$_REQUEST['search']?$_REQUEST['search']:"";
$url="http://www.google.co.in/search?num=$result_cnt&hl=en&safe=off&q={$search}";
curl_setopt($ch, CURLOPT_URL, $url);
//Becoz we dont want to show result by default
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$data=curl_exe($ch); //curl execute function will need to change to correct one
$data_arr=explode("
- ",$data);
$data=explode("
",$data_arr[1]);
print_r($data[0]);
die();
}
?>
//JAvascript Starts
//these script lines can be in a seperate js file
function GetXmlHttpObject(handler)
{
var objXMLHttp=null;
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}
xmlHttp=GetXmlHttpObject();
//***************
function get_data()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
document.getElementById("search_result").innerHTML= xmlHttp.responseText;
}
//***************
function search_data(txt)
{
url="ajaxsearch.php?search="+txt;
xmlHttp.onreadystatechange=get_data;
xmlHttp.open("POST",url,true) ;
xmlHttp.send(null);
}
//Javascript End
Search Results
| Attachment | Size |
|---|---|
| Code file for Ajax Based Simple Search | 1.83 KB |
![Validate my RSS feed [Valid RSS]](http://feedvalidator.org/images/valid-rss.png)