0

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 !

4
  • Questions like this are asked 10 times a day. Just watch the main page for a few minutes! Commented Jan 13, 2015 at 14:21
  • 1
    Done any basic debugging, like var_dump($bingGoogleArray) on the server? As well, note that scraping search results like this is a violation of Google's ToS. Commented Jan 13, 2015 at 14:25
  • It seems that you didn't understood me , the problem is when i send $googleSource alone it can be displayed in html . But if i send $googleSource and $bingSource in json encoded array only $bingSource can be displayed . Commented Jan 13, 2015 at 15:56
  • @Tomáš Zato i searched but the answer for my question don't exist . Commented Jan 13, 2015 at 16:01

1 Answer 1

0

i checked your source, and for me $googleSource is null. the error is: json_encode(): Invalid UTF-8 sequence in argument

so try with this line:

$googleSource = utf8_encode(@file_get_contents($googleUrl));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.