Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Add posability for dynamic configuration #112

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions SEOstats/Config/ApiKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,14 @@
* @copyright Copyright (c) 2010 - present Stephan Schmitz
* @license http://eyecatchup.mit-license.org/ MIT License
* @updated 2013/12/11
*/

/**
* Client API keys
* @package SEOstats
*/
interface ApiKeys
class ApiKeys extends ConfigAbstract
{
// To acquire an API key, visit Google's APIs Console here:
// https://code.google.com/apis/console
// In the Services pane, activate the "PageSpeed Insights API" (not the service!).
// Next, go to the API Access pane. The API key is near the bottom of that pane,
// in the section titled "Simple API Access.".
const GOOGLE_SIMPLE_API_ACCESS_KEY = '';

// To acquire a Mozscape (f.k.a. SEOmoz) API key, visit:
// https://moz.com/products/api/keys
const MOZSCAPE_ACCESS_ID = '';
const MOZSCAPE_SECRET_KEY = '';

// To acquire a SISTRIX API key, visit:
// http://www.sistrix.de
const SISTRIX_API_ACCESS_KEY = '';
protected static $config = [
'GOOGLE_SIMPLE_API_ACCESS_KEY' => '',
'MOZSCAPE_ACCESS_ID' => '',
'MOZSCAPE_SECRET_KEY' => '',
'SISTRIX_API_ACCESS_KEY' => '',
];
}
30 changes: 30 additions & 0 deletions SEOstats/Config/ConfigAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace SEOstats\Config;

abstract class ConfigAbstract
{
protected static $config = [];

public static function configure($config)
{
// Update only existed
foreach ($config as $key => $value) {
static::set($key, $config[$key]);
}
}

public static function set($name, $value)
{
if (isset(static::$config[$name])) {
static::$config[$name] = $value;
}
}

public static function get($name)
{
if (!isset(static::$config[$name])) {
throw new \Exception("Value for $name does not exist");
}
return static::$config[$name];
}
}
8 changes: 8 additions & 0 deletions SEOstats/Config/Configurable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace SEOstats\Config;

interface Configurable
{
public static function configure($config);
public static function get($value);
}
102 changes: 48 additions & 54 deletions SEOstats/Config/DefaultSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,55 @@
* @copyright Copyright (c) 2010 - present Stephan Schmitz
* @license http://eyecatchup.mit-license.org/ MIT License
* @updated 2013/08/17
*/

/**
* Default client settings
* @package SEOstats
*/
interface DefaultSettings
{
// The default value returned by all SEOstats methods if no result available.
// Can be either of type String, Int, Bool or NULL.
const DEFAULT_RETURN_NO_DATA = 'n.a.';

// The default top level domain ending to use to query Google.
const GOOGLE_TLD = 'com';

// The HTTP header value for the 'Accept-Language' attribute.
//
// Note: Google search results, doesn't matter which tld you request, vary depending on
// the value sent for the HTTP header attribute 'Accept-Language'! Eg: I am from Germany.
// Even if I use the "ncr" (no country redirect) request parameter, all search results
// that I get in response to a query on google.com will be localized to German, because
// my browser sends an Accept-Language header value of 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3'.
// On the other side, if I change my browser settings to send a value of 'en-us;q=0.8,en;q=0.3',
// all my searches on google.de (the german Google page) will be localized English.
// Thus, if you want to get the same results that you see when you search Google from
// your browser, you must not only set the @const GOOGLE_TLD to your country specifiy TLD,
// but also set the value below to be the same used by your browser!
const HTTP_HEADER_ACCEPT_LANGUAGE = 'en-us;q=0.8,en;q=0.3';

// For curl instances: Whether to allow Google to store cookies, or not.
const ALLOW_GOOGLE_COOKIES = 0;

// Choose the local SEMRush database to use.
// Valid values are:
// au - Google.com.au (Australia)
// br - Google.com.br (Brazil)
// ca - Google.ca (Canada)
// de - Google.de (Germany)
// es - Google.es (Spain)
// fr - Google.fr (France)
// it - Google.it (Italy)
// ru - Google.ru (Russia)
// uk - Google.co.uk (United Kingdom)
// us - Google.com (United States)
const SEMRUSH_DB = 'us';

// Choose the local SISTRIX database to use.
// Valid values are:
// de – Germany
// at – Austria
// ch – Switzerland
// us – USA
// uk – England
// es – Spain
// fr – France
// it – Italy
const SISTRIX_DB = 'de';
class DefaultSettings extends ConfigAbstract
{
protected static $config = [
// The default value returned by all SEOstats methods if no result available.
// Can be either of type String, Int, Bool or NULL.
'DEFAULT_RETURN_NO_DATA' => 'n.a.',
// The default top level domain ending to use to query Google.
'GOOGLE_TLD' => 'com',
// The HTTP header value for the 'Accept-Language' attribute.
//
// Note: Google search results, doesn't matter which tld you request, vary depending on
// the value sent for the HTTP header attribute 'Accept-Language'! Eg: I am from Germany.
// Even if I use the "ncr" (no country redirect) request parameter, all search results
// that I get in response to a query on google.com will be localized to German, because
// my browser sends an Accept-Language header value of 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3'.
// On the other side, if I change my browser settings to send a value of 'en-us;q=0.8,en;q=0.3',
// all my searches on google.de (the german Google page) will be localized English.
// Thus, if you want to get the same results that you see when you search Google from
// your browser, you must not only set the @const GOOGLE_TLD to your country specifiy TLD,
// but also set the value below to be the same used by your browser!
'HTTP_HEADER_ACCEPT_LANGUAGE' => 'en-us;q=0.8,en;q=0.3',
// For curl instances: Whether to allow Google to store cookies, or not.
'ALLOW_GOOGLE_COOKIES' => 0,
// Choose the local SEMRush database to use.
// Valid values are:
// au - Google.com.au (Australia)
// br - Google.com.br (Brazil)
// ca - Google.ca (Canada)
// de - Google.de (Germany)
// es - Google.es (Spain)
// fr - Google.fr (France)
// it - Google.it (Italy)
// ru - Google.ru (Russia)
// uk - Google.co.uk (United Kingdom)
// us - Google.com (United States)
'SEMRUSH_DB' => 0,
// Choose the local SISTRIX database to use.
// Valid values are:
// de – Germany
// at – Austria
// ch – Switzerland
// us – USA
// uk – England
// es – Spain
// fr – France
// it – Italy
'SISTRIX_DB' => 0
];
}
2 changes: 1 addition & 1 deletion SEOstats/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface Services
const SEMRUSH_WIDGET_URL = 'http://widget.semrush.com/widget.php?action=report&type=%s&db=%s&domain=%s';

// Mozscape (f.k.a. Seomoz) Link metrics API Endpoint.
const MOZSCAPE_API_URL = 'http://lsapi.seomoz.com/linkscape/url-metrics/%s?Cols=%s&AccessID=%s&Expires=%s&Signature=%s';
const MOZSCAPE_API_URL = 'http://lsapi.seomoz.com/linkscape/url-metrics/%s?Cols=103079233536&AccessID=%s&Expires=%s&Signature=%s';

// Google Websearch API Endpoint.
const GOOGLE_APISEARCH_URL = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=%s&q=%s';
Expand Down
60 changes: 31 additions & 29 deletions SEOstats/Helper/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ class HttpRequest
{
/**
* HTTP GET/POST request with curl.
* @access public
* @param String $url The Request URL
* @param Array $postData Optional: POST data array to be send.
* @return Mixed On success, returns the response string.
* @access public
* @param String $url The Request URL
* @param Array $postData Optional: POST data array to be send.
* @return Mixed On success, returns the response string.
* Else, the the HTTP status code received
* in reponse to the request.
*/
public static function sendRequest($url, $postData = false, $postJson = false)
{
$ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
\SEOstats\SEOstats::BUILD_NO);
\SEOstats\SEOstats::BUILD_NO);

$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
));

if (false !== $postData) {
Expand All @@ -52,32 +52,34 @@ public static function sendRequest($url, $postData = false, $postJson = false)
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if (429 == (int)$httpCode) {
throw new TooManyRequestsException("To many requests");
}
return (200 == (int)$httpCode) ? $response : false;
}

/**
* HTTP HEAD request with curl.
*
* @access private
* @param String $a The request URL
* @param String $a The request URL
* @return Integer Returns the HTTP status code received in
* response to a GET request of the input URL.
*/
public static function getHttpCode($url)
{
$ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
\SEOstats\SEOstats::BUILD_NO);
\SEOstats\SEOstats::BUILD_NO);

$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_NOBODY => 1,
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_NOBODY => 1,
));

curl_exec($ch);
Expand All @@ -90,19 +92,19 @@ public static function getHttpCode($url)
public function getFile($url, $file)
{
$ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
\SEOstats\SEOstats::BUILD_NO);
\SEOstats\SEOstats::BUILD_NO);

$fp = fopen("$file", 'w');

$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_FILE => $fp,
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_FILE => $fp,
));

curl_exec($ch);
Expand Down
8 changes: 8 additions & 0 deletions SEOstats/Helper/TooManyRequestsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace SEOstats\Helper;

use yii\console\Exception;

class TooManyRequestsException extends Exception{

};
Loading