Skip to content

Instantly share code, notes, and snippets.

@s4l1h
Created June 15, 2012 09:13
Show Gist options
  • Save s4l1h/2935545 to your computer and use it in GitHub Desktop.
Save s4l1h/2935545 to your computer and use it in GitHub Desktop.
Google Search Class
<?php
header('Content-type: text/html; charset=UTF-8');
include('GoogleSearch.php');
$kelime="Ahmet Kaya";
$google=new GoogleSearch();
$google->setLang("tr");
if($google->searchRelated($kelime)){
print_r($google->getResultRelated($kelime));
}
// Şimdilik Sadece Benzer Kelimeleri Verebiliyor.
<?php
class GoogleSearch{
public $object_cache_related=array();
public $lang="tr";
public function __construct(){
}
protected function _getData($url, $ref = FALSE)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Ubuntu; X11; Linux x86_64; rv:8.0) Gecko/20100101 Firefox/8.0");
$ref = $ref !== FALSE ? $ref : 'http://www.google.com/';
curl_setopt($curl, CURLOPT_REFERER, $ref);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
$source = curl_exec($curl);
if ($source === FALSE) {
return FALSE;
}
return $source;
}
protected function _googleRelated($keyword){
$kaynak=$this->_getData("http://www.google.com/search?hl=".$this->lang."&q=".urlencode($keyword));
if($kaynak===FALSE){
throw new Exception("Google Üzerinden Veri Alınamadı");
}
$d='@\<div class\=brs_col\>(.*)\</div\>@siU';
$dd='@\<a href\=".*?" \>(.*?)\</a\>@';
preg_match_all($d,$kaynak,$sonuc);
$result=array();
if(count($sonuc['1']) > 0){
foreach($sonuc['1'] AS $s){
preg_match_all($dd,$s,$ss);
if(count($ss['1']) > 0){
foreach($ss['1'] AS $sss){
$sss=trim(strip_tags($sss));
if(in_array($sss,$result)===FALSE){
$result[]=$sss;
}
}
}
}
}
return count($result)>0 ? $result : False;
}
public function searchRelated($keyword=NULL){
if($keyword===NULL){
throw new Exception("searchRelated fonksiyonu NULL değer alamaz");
}
$this->object_cache_related[$keyword]=$this->_googleRelated($keyword);
return $this->object_cache_related[$keyword]!=False ? True :False;
}
public function getResultRelated($keyword=NULL){
if($keyword===NULL){
throw new Exception("getResultRelated fonksiyonu NULL değer alamaz");
}
return isset($this->object_cache_related[$keyword]) ?$this->object_cache_related[$keyword] : False;
}
public function setLang($lang=NULL){
if($lang===NULL){
throw new Exception("setLang fonksiyonu NULL değer alamaz");
}
# TODO: Geçerli Diller Dizisi Eklenecek in_array($lang,$valid_lang)
$this->lang=$lang;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment