├── .gitattributes ├── .gitignore ├── RankChecker.class.php ├── index.php └── readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | [Dd]ebug/ 46 | [Rr]elease/ 47 | *_i.c 48 | *_p.c 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.vspscc 63 | .builds 64 | *.dotCover 65 | 66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 67 | #packages/ 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | 76 | # Visual Studio profiler 77 | *.psess 78 | *.vsp 79 | 80 | # ReSharper is a .NET coding add-in 81 | _ReSharper* 82 | 83 | # Installshield output folder 84 | [Ee]xpress 85 | 86 | # DocProject is a documentation generator add-in 87 | DocProject/buildhelp/ 88 | DocProject/Help/*.HxT 89 | DocProject/Help/*.HxC 90 | DocProject/Help/*.hhc 91 | DocProject/Help/*.hhk 92 | DocProject/Help/*.hhp 93 | DocProject/Help/Html2 94 | DocProject/Help/html 95 | 96 | # Click-Once directory 97 | publish 98 | 99 | # Others 100 | [Bb]in 101 | [Oo]bj 102 | sql 103 | TestResults 104 | *.Cache 105 | ClientBin 106 | stylecop.* 107 | ~$* 108 | *.dbmdl 109 | Generated_Code #added for RIA/Silverlight projects 110 | 111 | # Backup & report files from converting an old project file to a newer 112 | # Visual Studio version. Backup files are not needed, because we have git ;-) 113 | _UpgradeReport_Files/ 114 | Backup*/ 115 | UpgradeLog*.XML 116 | 117 | 118 | 119 | ############ 120 | ## Windows 121 | ############ 122 | 123 | # Windows image file caches 124 | Thumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | 130 | ############# 131 | ## Python 132 | ############# 133 | 134 | *.py[co] 135 | 136 | # Packages 137 | *.egg 138 | *.egg-info 139 | dist 140 | build 141 | eggs 142 | parts 143 | bin 144 | var 145 | sdist 146 | develop-eggs 147 | .installed.cfg 148 | 149 | # Installer logs 150 | pip-log.txt 151 | 152 | # Unit test / coverage reports 153 | .coverage 154 | .tox 155 | 156 | #Translations 157 | *.mo 158 | 159 | #Mr Developer 160 | .mr.developer.cfg 161 | 162 | # Mac crap 163 | .DS_Store 164 | -------------------------------------------------------------------------------- /RankChecker.class.php: -------------------------------------------------------------------------------- 1 | start = $start; 15 | $this->end = $end; 16 | } 17 | 18 | public function find($keyword, $useproxie, $proxies) 19 | { 20 | $results = []; 21 | 22 | for ($start = ($this->start-1) * 10; $start <= $this->end * 10; $start += 10) { 23 | $ua = [ 24 | 0 => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201', 25 | 10 => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', 26 | 20 => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1' 27 | ]; 28 | 29 | if ($useproxie) { 30 | $host = $proxies['host']; 31 | $port = $proxies['port']; 32 | $username = $proxies['username']; 33 | $password = $proxies['password']; 34 | 35 | if (!empty($username)) { 36 | $auth = base64_encode($username . ':' . $password); 37 | $useauth = sprintf('Proxy-Authorization: Basic %s', $auth); 38 | } else { 39 | $useauth = ''; 40 | } 41 | 42 | $options = [ 43 | 'http' => [ 44 | 'method' => 'GET', 45 | 'header' => "Accept-language: en\r\n" . 46 | "Cookie: PHP Google Keyword Position\r\n" . 47 | "User-Agent: " . $ua[$start] . "\r\n". 48 | $useauth, 49 | 'proxy' => sprintf('tcp://%s:%s', $host, $port), 50 | 'request_fulluri' => true 51 | ] 52 | ]; 53 | } else { 54 | $options = [ 55 | 'http' => [ 56 | 'method' => 'GET', 57 | 'header' => "Accept-language: en\r\n" . 58 | "Cookie: PHP Google Keyword Position\r\n" . 59 | "User-Agent: " . $ua[$start] 60 | ] 61 | ]; 62 | } 63 | 64 | if ($useproxie) { 65 | if (!empty($username)) { 66 | $auth = base64_encode($username . ':' . $password); 67 | $arrayproxies = [ 68 | CURLOPT_PROXY => $host, 69 | CURLOPT_PROXYPORT => $port, 70 | CURLOPT_PROXYUSERPWD => $auth 71 | ]; 72 | } else { 73 | $arrayproxies = [ 74 | CURLOPT_PROXY => $host, 75 | CURLOPT_PROXYPORT => $port 76 | ]; 77 | } 78 | } else { 79 | $arrayproxies = []; 80 | } 81 | 82 | $keyword = str_replace(' ', '+', trim($keyword)); 83 | $url = sprintf('https://www.google.com/search?ie=UTF-8&q=%s&start=%s&num=30', $keyword, $start); 84 | $context = stream_context_create($options); 85 | 86 | if ($this->_isCurlEnabled()) { 87 | $data = $this->_curl($url, $useproxie, $arrayproxies); 88 | } else { 89 | $data = @file_get_contents($url, false, $context); 90 | } 91 | 92 | if (is_array($data)) { 93 | $errmsg = $data['errmsg']; 94 | $results = ['rank' => 'zero', 'url' => $errmsg]; 95 | } else { 96 | if (strpos($data, 'To continue, please type the characters below') !== false || $data == false 97 | || strpos($data, "We're sorry") !== false ) { 98 | $results = ['rank' => 'zero', 'url' => '']; 99 | } else { 100 | $j = -1; 101 | $i = 1; 102 | 103 | while (($j = stripos($data, '', $j+1)) !== false) { 104 | $k = stripos($data, '', $j); 105 | $link = strip_tags(substr($data, $j, $k-$j)); 106 | $rank = $i++; 107 | $results[] = ['rank' => $rank, 'url' => $link]; 108 | } 109 | } 110 | } 111 | 112 | $sleep = rand(20, 25); 113 | sleep($sleep); 114 | } 115 | 116 | return $results; 117 | } 118 | 119 | private function _isCurlEnabled() 120 | { 121 | return function_exists('curl_version'); 122 | } 123 | 124 | private function _curl($url, $useproxie, $arrayproxies) 125 | { 126 | try { 127 | $ch = curl_init($url); 128 | 129 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 130 | curl_setopt($ch, CURLOPT_HEADER, false); 131 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 132 | curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'); 133 | curl_setopt($ch, CURLOPT_AUTOREFERER, true); 134 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); 135 | curl_setopt($ch, CURLOPT_TIMEOUT, 120); 136 | curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 137 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 138 | curl_setopt($ch, CURLOPT_SSLVERSION, 'all'); 139 | 140 | if ($useproxie) { 141 | if (!empty($arrayproxies)) { 142 | foreach($arrayproxies as $param => $val) { 143 | curl_setopt($ch, $param, $val); 144 | } 145 | } 146 | } 147 | 148 | $content = curl_exec($ch); 149 | $errno = curl_errno($ch); 150 | $error = curl_error($ch); 151 | curl_close($ch); 152 | 153 | if (!$errno) { 154 | return $content; 155 | } else { 156 | return [ 157 | 'errno' => $errno, 158 | 'errmsg'=> $error 159 | ]; 160 | } 161 | } catch (Exception $e) { 162 | return [ 163 | 'errno' => $e->getCode(), 164 | 'errmsg' => $e->getMessage() 165 | ]; 166 | } 167 | } 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 'HOST', 'port' => 'PORT', 'username' => 'USERNAME', 'password' => 'PASSWORD'); 5 | */ 6 | include('RankChecker.class.php'); 7 | 8 | $newGoogleRankChecker = new GoogleRankChecker(); 9 | $newquery = 'cat'; 10 | $useproxies = false; 11 | $arrayproxies = []; 12 | $googledata = $newGoogleRankChecker->find($newquery, $useproxies, $arrayproxies); 13 | 14 | echo '
'; var_dump($googledata);
15 | 


--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
 1 | ## PHP Google Keyword Position
 2 | 
 3 | Modified by
 4 | 
 5 | Name    : Teguh Suandi
6 | Email : mtasuandi@outlook.com 7 | 8 | This PHP Class is a modified class from the PHP Class made by Hamed Afshar from http://www.golha.net. I just add one more result to get the keyword position, previously this function only has result page position and the url. 9 | 10 | CHANGELOGS: 11 |

12 | Version 1.3 (2016/10/26) 13 |

17 |

18 |

19 | Version 1.2 (2015/07/02) 20 |

24 |

25 |

26 | Version 1.1 (2013/13/02) 27 |

30 |

31 |

32 | Version 1.0 (2011/08/02) 33 |

36 |

37 | --------------------------------------------------------------------------------