Инструмент проверки поиска в Google на PHP

Я пытаюсь сделать инструмент, который ищет в Google положение многих ключевых слов в PHP. Он работает нормально, но всегда показывает немного неправильные результаты (показывает результаты для google.de). Я проверил код локали и, кажется, все в порядке. Пожалуйста, сообщите мне, если в этом коде есть ошибка. Вот код для функции проверки рейтинга страницы:

<?php

Class RankingChecker {

private $googleApiKey = null;

private $googleBaseUrl = 'http://ajax.googleapis.com/ajax/services/search/web?         v=1.0';

private $checkPageCount = null;

private $language = 'en';

private $country = 'IN';


/**
 * get an api key from: http://code.google.com/apis/ajaxsearch/signup.html
 * @param string $googleApiKey
 * @param int $checkPageCount
 */
public function __construct($googleApiKey, $checkPageCount = 5) {
    $this->googleApiKey = $googleApiKey;
    $this->checkPageCount = $checkPageCount;
}


/**
 * set language and country 
 * @param string $language
 * @param string $country
 */
public function setLocale($language, $country = null) {
    $this->country = $country;
    $this->language = $language;
}


/**
 * get rankings
 * @example $checker->check(array('bohuco'), array('bohuco.net'));
 * @param array $keywords search these keywords
 * @param array $domains domains to compare against
 */
public function check($keywords, $domains) {

    $rankings = array();

    if (! is_array($keywords)) { throw new Exception('Keywords array is no array'); }
    if (! is_array($domains)) { throw new Exception('Domains array is no array'); }

    foreach($keywords as $keyword) {
        $keyword = trim($keyword);
        $rows = array();

        if ($keyword) {
            for($i=0;$i<$this->checkPageCount;$i++) {
                $start = $i*8;

                if (! empty($this->language)) {
                    $this->language = '&gl='.$this->language;
                }
                if (! empty($this->country)) {
                    $this->country = '&gl='.$this->country;
                }
                $url = sprintf('%s&hl=%s%s&q=%s&rsz=8&key=%s&start=%s', $this->googleBaseUrl, $this->language, $this->country, urlencode($keyword), $this->googleApiKey, $start);
                if ($result = file_get_contents($url)) {
                    $result = json_decode($result);
                    $rows = array_merge($rows, $result->responseData->results);
                }
            }

            foreach($domains as $url) {
                $rankings[$keyword][$url] = '-';
                foreach($rows as $position => $row) {
                    if (strpos($row->url, trim($url)) !== false) {
                        $rankings[$keyword][$url] = $position+1;
                        break;
                    }
                }
            }
        }       
    }   

    return $rankings;
        }

}

Вот код, где я использую функцию:

<?php

error_reporting(E_ALL);
ini_set('display_errors', true);

if (isset($_GET['source'])) { show_source(__FILE__); exit; }

if (isset($_POST['keywords']) && ! empty($_POST['keywords']) &&
    isset($_POST['domains']) && ! empty($_POST['domains'])) {

    $apiKey = 'AIzaSyCvMbBacxD_itDhyEchD9jPUKDZxLvUVa8';

    require_once 'RankingChecker.php';
    $rankingChecker = new RankingChecker($apiKey);

    if ($_POST['language'] && $_POST['country']) {
        $rankingChecker->setLocale($_POST['language'], $_POST['country']);
    }

    $keywords = array_splice(explode("\n", $_POST['keywords']), 0, 5);
    $domains = explode("\n", $_POST['domains']);

    $rankings = $rankingChecker->check($keywords, $domains);

}

?>

0 ответов

Другие вопросы по тегам