• Fetch Google search data inside your site using curl

    Create test.php in your folder (i.e. googlesearch here) inside htdocs.

    test.php

     

    ” >
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    
    <!--?php 
     
    function google_search_api($args, $referer = 'http://localhost/googlesearch/', $endpoint = 'web')
     
    {
     
        $url = "http://ajax.googleapis.com/ajax/services/search/".$endpoint;
     
        if ( !array_key_exists('v', $args) )
     
        $args['v'] = '1.0';
     
        $args['rsz']='8'; //Max value 8
     
        $url .= '?'.http_build_query($args, '', '&#038;');
     
        $ch = curl_init();
     
        curl_setopt($ch, CURLOPT_URL, $url);
     
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     
        // note that the referer *must* be set
     
        curl_setopt($ch, CURLOPT_REFERER, $referer);
     
        $body = curl_exec($ch);
     
        curl_close($ch);
     
        //decode and return the response
     
        return json_decode($body,true);
     
    }
     
    $query=urldecode(isset($_GET['q'])?$_GET['q']:"none");
     
    echo "Results for: $query<br ?-->-----
    ";
     
    $res = google_search_api(array('q' =&gt; $query));
     
    print_r($res['responseData']['results']);
     
    ?&gt;

    Run test.php in browser, insert your query in input box, click on ‘Search’ and Google search result for that query will be printed in array format.

    Tags:

Comments on this post

Leave a Reply

  • Security Code :


    8 × = sixty four