This is the php script :
<?php
//this line is necessary to make JS and php communicate with json
//header("Content-type: text/javascript");
//$searchQuery = "facebook";
$searchQuery = $_GET['q'];
$googleUrl='http://www.google.com/search?q='.$searchQuery;
$googleSource = @file_get_contents($googleUrl);
$bingUrl = 'http://www.bing.com/search?q=' .$searchQuery.'&first=1';
$bingSource = @file_get_contents($bingUrl);
$bingGoogleArray = array($bingSource,$googleSource);
echo json_encode($bingGoogleArray);
//echo $googleSource;
?>
And this is the html page :
<html>
<head>
<title>Test Ajax And Jquery</title>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script>
<script>
function ajaxTest() {
//var string = "<html><body><b>Hello World</b></body></html>";
//$('#test').html(string);
$.ajax({url:"testJqueryAjax.php?q=facebook" , success: function(result){
$('h1').empty();
//var data = JSON.parse(result);
var data = $.parseJSON(result);
$('p').html(data[1]);
//alert(data[1]);
}});
}
</script>
</head>
<body>
<div id="search" style="height:15%;">
<button onClick="ajaxTest()">Submit</button>
<h1>Loading</h1>
</div>
<p></p>
</body>
</html>
The role of this little web application it that the php script search a keyword on google and bing and the return a json encoded array that contains the html source of the bing search page and google page.
On the html page i parse the json_encoded array that I received from the php script and i copy data[i] into a html tag .
The problem is when I copy data[0]
that contains Bing search page into html tag the result appears but when I copy data[1]
that contains Google search page into html tag nothing appears ! :(
You may say the $googleSource
is empty but when I send from the php script $googleSource alone it works !
var_dump($bingGoogleArray)
on the server? As well, note that scraping search results like this is a violation of Google's ToS.