11/07/2016

PHP : ดึงผลลัพธ์การค้นหาของ google

PHP : ดึงผลลัพธ์การค้นหาของ google
  1. $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=0&rsz=8&hl=th&q=";
  2. $q = urlencode("วาล์วกันกลับ");
  3.  
  4. // sendRequest
  5. // note how referer is set manually
  6. $ch = curl_init();
  7. curl_setopt($ch, CURLOPT_URL, $url.$q);
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  9. //curl_setopt($ch, CURLOPT_REFERER, "http://www.scivalve.com");
  10. $body = curl_exec($ch);
  11. curl_close($ch);
  12.  
  13. // now, process the JSON string
  14. $json = json_decode($body, true);
  15. // now have some fun with the results...
  16.  
  17. foreach ($json as $item)
  18. {
  19.      //var_dump($item);
  20.      //echo "<hr>";
  21.      if(is_array($item))
  22.     foreach ($item["results"] as $result)
  23.     {
  24.         echo "<hr>";
  25.         var_dump($result);
  26.     }
  27.    
  28. }


https://developers.google.com/web-search/docs/?hl=en

No comments:

Post a Comment