PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/SEOstats/SEOstats.php

http://github.com/eyecatchup/SEOstats
PHP | 272 lines | 150 code | 32 blank | 90 comment | 11 complexity | 9c9e34036ac6505163f902ea1fd2b757 MD5 | raw file
  1. <?php
  2. namespace SEOstats;
  3. use SEOstats\Common\SEOstatsException as E;
  4. use SEOstats\Config as Config;
  5. use SEOstats\Helper as Helper;
  6. use SEOstats\Services as Service;
  7. /** SEOstats
  8. * ================================================================================
  9. * PHP library to request a bunch of SEO-relevant metrics, such as looking up the
  10. * visibilty of a URL within organic search results, Pagespeed analysis, the
  11. * Google Toolbar PageRank, Page-Authority, Backlink-Details, Traffic Statistics,
  12. * social media relevance, comparing competing websites and a lot more.
  13. * ================================================================================
  14. * @package SEOstats
  15. * @author Stephan Schmitz <eyecatchup@gmail.com>
  16. * @copyright Copyright (c) 2010 - present Stephan Schmitz
  17. * @license http://eyecatchup.mit-license.org
  18. * @version CVS: $Id: SEOstats.php, v2.5.2 Rev 31 2013/08/14 13:57:17 ssc Exp $
  19. * @link https://github.com/eyecatchup/SEOstats/
  20. * ================================================================================
  21. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining
  22. * a copy of this software and associated documentation files (the "Software'),
  23. * to deal in the Software without restriction, including without limitation the
  24. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  25. * copies of the Software, and to permit persons to whom the Software is furnished
  26. * to do so, subject to the following conditions:
  27. *
  28. * The above copyright notice and this permission notice shall be included in all
  29. * copies or substantial portions of the Software.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  34. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY
  35. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  36. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. * ================================================================================
  38. */
  39. /**
  40. * Check required PHP settings.
  41. */
  42. if (!function_exists('curl_init')) {
  43. throw new E('SEOstats requires the PHP CURL extension.');
  44. exit();
  45. }
  46. if (1 == ini_get('safe_mode') || 'on' === strtolower(ini_get('safe_mode'))) {
  47. throw new E('Because some SEOstats functions require the CURLOPT_FOLLOWLOCATION flag, ' .
  48. 'you must not run PHP in safe mode! (This flag can not be set in safe mode.)');
  49. exit();
  50. }
  51. /**
  52. * Starting point for the SEOstats library. Example Usage:
  53. *
  54. * <code>
  55. * ...
  56. * $url = 'http://www.domain.tld';
  57. *
  58. * // Get the Google Toolbar PageRank value.
  59. * $result = \SEOstats\Services\Google::getPageRank($url);
  60. *
  61. * // Get the first 100 results for a Google search for 'query string'.
  62. * $result = \SEOstats\Services\Google::getSerps('query string');
  63. *
  64. * // Get the first 500 results for a Google search for 'query string'.
  65. * $result = \SEOstats\Services\Google::getSerps('query string', 500);
  66. *
  67. * // Check the first 500 results for a Google search for 'query string' for
  68. * // occurrences of the given domain name and return an array of matching
  69. * // URL's and their position within the serps.
  70. * $result = \SEOstats\Services\Google::getSerps('query string', 500, $url);
  71. * ...
  72. * </code>
  73. *
  74. */
  75. class SEOstats
  76. {
  77. const BUILD_NO = Config\Package::VERSION_CODE;
  78. protected static $_url,
  79. $_host,
  80. $_lastHtml,
  81. $_lastLoadedUrl,
  82. $_curlopt_proxy,
  83. $_curlopt_proxyuserpwd,
  84. $_ua
  85. = false;
  86. public function __construct($url = false)
  87. {
  88. if (false !== $url) {
  89. self::setUrl($url);
  90. }
  91. }
  92. public function Alexa()
  93. {
  94. return new Service\Alexa;
  95. }
  96. public function Google()
  97. {
  98. return new Service\Google;
  99. }
  100. public function Mozscape()
  101. {
  102. return new Service\Mozscape;
  103. }
  104. public function OpenSiteExplorer()
  105. {
  106. return new Service\OpenSiteExplorer;
  107. }
  108. public function SEMRush()
  109. {
  110. return new Service\SemRush;
  111. }
  112. public function Sistrix()
  113. {
  114. return new Service\Sistrix;
  115. }
  116. public function Social()
  117. {
  118. return new Service\Social;
  119. }
  120. public static function getLastLoadedHtml()
  121. {
  122. return self::$_lastHtml;
  123. }
  124. public static function getLastLoadedUrl()
  125. {
  126. return self::$_lastLoadedUrl;
  127. }
  128. /**
  129. * Ensure the URL is set, return default otherwise
  130. * @return string
  131. */
  132. public static function getUrl($url = false)
  133. {
  134. $url = false !== $url ? $url : self::$_url;
  135. return $url;
  136. }
  137. public function setUrl($url)
  138. {
  139. if (false !== Helper\Url::isRfc($url)) {
  140. self::$_url = $url;
  141. self::$_host = Helper\Url::parseHost($url);
  142. }
  143. else {
  144. throw new E('Invalid URL!');
  145. exit();
  146. }
  147. return true;
  148. }
  149. public static function getHost($url = false)
  150. {
  151. return Helper\Url::parseHost(self::getUrl($url));
  152. }
  153. public static function getDomain($url = false)
  154. {
  155. return 'http://' . self::getHost($url = false);
  156. }
  157. /**
  158. * @return DOMDocument
  159. */
  160. protected static function _getDOMDocument($html) {
  161. $doc = new \DOMDocument;
  162. @$doc->loadHtml($html);
  163. return $doc;
  164. }
  165. /**
  166. * @return DOMXPath
  167. */
  168. protected static function _getDOMXPath($doc) {
  169. $xpath = new \DOMXPath($doc);
  170. return $xpath;
  171. }
  172. /**
  173. * @return HTML string
  174. */
  175. protected static function _getPage($url) {
  176. $url = self::getUrl($url);
  177. if (self::getLastLoadedUrl() == $url) {
  178. return self::getLastLoadedHtml();
  179. }
  180. $html = Helper\HttpRequest::sendRequest($url);
  181. if ($html) {
  182. self::$_lastLoadedUrl = $url;
  183. self::_setHtml($html);
  184. return $html;
  185. }
  186. else {
  187. self::noDataDefaultValue();
  188. }
  189. }
  190. protected static function _setHtml($str)
  191. {
  192. self::$_lastHtml = $str;
  193. }
  194. protected static function noDataDefaultValue()
  195. {
  196. return Config\DefaultSettings::DEFAULT_RETURN_NO_DATA;
  197. }
  198. /**
  199. * @return Proxy address
  200. */
  201. public static function getCurloptProxy()
  202. {
  203. return self::$_curlopt_proxy;
  204. }
  205. /**
  206. * @param Proxy address $curlopt_proxy
  207. */
  208. public static function setCurloptProxy($curlopt_proxy)
  209. {
  210. self::$_curlopt_proxy = $curlopt_proxy;
  211. }
  212. /**
  213. * @return Proxy auth
  214. */
  215. public static function getCurloptProxyuserpwd()
  216. {
  217. return self::$_curlopt_proxyuserpwd;
  218. }
  219. /**
  220. * @param Proxy auth $curlopt_proxyuserpwd
  221. */
  222. public static function setCurloptProxyuserpwd($curlopt_proxyuserpwd)
  223. {
  224. self::$_curlopt_proxyuserpwd = $curlopt_proxyuserpwd;
  225. }
  226. /**
  227. * @return Useragent string
  228. */
  229. public static function getUserAgent()
  230. {
  231. return self::$_ua;
  232. }
  233. /**
  234. * @param Useragent string $ua
  235. */
  236. public static function setUserAgent($ua)
  237. {
  238. self::$_ua = $ua;
  239. }
  240. }