如何将SEOStats与Codeigniter集成?

时间:2013-11-01 11:58:24

标签: php codeigniter pagerank alexa

您好我想将SEOStats Class与codeigniter中的项目集成,是否有人为我提供解决方案?

我试图将SEOstats类作为帮助程序并在特定的控制器中加载帮助程序,但是显示一个空白页面,我也尝试通过视图包含它,但是我看到的是同一个空白页面,

我已将此代码包含在我的视图文件中,SEOstats目录也位于相同的视图目录中。

    <?php
    require_once  'SEOstats/bootstrap.php';

  use \SEOstats\Services as SEOstats;

  try {
    $url = 'http://www.google.com/';

 // Create a new SEOstats instance.
 $seostats = new \SEOstats\SEOstats;

  // Bind the URL to the current SEOstats instance.
  if ($seostats->setUrl($url)) {

   echo SEOstats\Alexa::getGlobalRank();
   echo SEOstats\Google::getPageRank();
 }
 }
 catch (SEOstatsException $e) {
 die($e->getMessage());
 }

我也用它作为库

<?php
 namespace SEOstats;

 use SEOstats\Common\SEOstatsException as E;
 use SEOstats\Config as Config;
 use SEOstats\Helper as Helper;
use SEOstats\Services as Service;



 class SEOstats
 {
   const BUILD_NO = Config\Package::VERSION_CODE;

protected static $_url,
                 $_host,
                 $_lastHtml,
                 $_lastLoadedUrl
                 = false;

public function __construct($url = false)
{
    if (false !== $url) {
        self::setUrl($url);
    }
}

public function Alexa()
{
    return new Service\Alexa;
}

public function Google()
{
    return new Service\Google;
}

public function OpenSiteExplorer()
{
    return new Service\OpenSiteExplorer;
}

public function SEMRush()
{
    return new Service\SemRush;
}

public function Sistrix()
{
    return new Service\Sistrix;
}

public function Social()
{
    return new Service\Social;
}

public static function getHost()
{
    return self::$_host;
}

public static function getLastLoadedHtml()
{
    return self::$_lastHtml;
}

public static function getLastLoadedUrl()
{
    return self::$_lastLoadedUrl;
}

/**
 * Ensure the URL is set, return default otherwise
 * @return string
 */
public static function getUrl($url = false)
{
    $url = false !== $url ? $url : self::$_url;
    return $url;
}

public function setUrl($url)
{
    if (false !== Helper\Url::isRfc($url)) {
        self::$_url  = $url;
        self::$_host = Helper\Url::parseHost($url);
    }
    else {
        throw new E('Invalid URL!');
        exit();
    }
    return true;
}

/**
 * @return DOMDocument
 */
protected static function _getDOMDocument($html) {
    $doc = new \DOMDocument;
    @$doc->loadHtml($html);
    return $doc;
}

/**
 * @return DOMXPath
 */
protected static function _getDOMXPath($doc) {
    $xpath = new \DOMXPath($doc);
    return $xpath;
}

/**
 * @return HTML string
 */
protected static function _getPage($url) {
    $url = self::getUrl($url);
    if (self::getLastLoadedUrl() == $url) {
        return self::getLastLoadedHtml();
    }

    $html = Helper\HttpRequest::sendRequest($url);
    if ($html) {
        self::$_lastLoadedUrl = $url;
        self::_setHtml($html);
        return $html;
    }
    else {
        self::noDataDefaultValue();
    }
}

protected static function _setHtml($str)
{
    self::$_lastHtml = $str;
}

protected static function noDataDefaultValue()
{
    return Config\DefaultSettings::DEFAULT_RETURN_NO_DATA;
}

  }

并将库加载为

$this->load->library('SEOstats');

2 个答案:

答案 0 :(得分:1)

我知道这篇文章很老了。但我最近也在寻找一个解决方案,并最终编写了自己的解决方案,并认为我会留在这里,以防其他人在将来寻找解决方案。

如果需要,请将以下内容放在库文件中并自动加载。

if (!defined('BASEPATH'))
   exit('No direct script access allowed');

class SEOstatistics {

   private $seostats;

   function __construct() {
      require_once( APPPATH . 'third_party/seostats/bootstrap.php' );

      $this->seostats = new \SEOstats\SEOstats;
   }

   private function alexa() {
      return new \SEOstats\Services\Alexa;
   }

   private function google() {
      return new \SEOstats\Services\Google;
   }

   private function moz() {
      return new \SEOstats\Services\Mozscape();
   }

   private function openSiteExplorer() {
      return new \SEOstats\Services\OpenSiteExplorer();
   }

   private function semRush() {
      return new \SEOstats\Services\SemRush();
   }

   private function sistrix() {
      return new \SEOstats\Services\Sistrix();
   }

   private function social() {
      return new \SEOstats\Services\Social();
   }

   public function __call($method, $url) {
       if (method_exists($this, $method)) {
          if ($this->seostats->setUrl($url[0])) {
             return call_user_func_array(array($this, $method),array());
          }

          return false;
       }
   }
}

然后在控制器或模型中使用它的一个例子是:

$google = $this->seostatistics->google($url);
$rank = $google->getPageRank();

答案 1 :(得分:0)

这就是我在Codeigniter网站上包含SEOStats的方式

class Cron extends Frontend_Controller
{

    public function get_google_page_rank() {
        require_once (APPPATH . 'libraries/SEOstats/bootstrap.php');
        try {
            $url = 'http://www.google.com/';

            // Get the Google PageRank for the given URL.
            $pagerank = \SEOstats\Services\Google::getPageRank($url);
            echo "The current Google PageRank for {$url} is {$pagerank}." . PHP_EOL;
        }
        catch(\Exception $e) {
            echo 'Caught SEOstatsException: ' . $e->getMessage();
        }
    }
    public function get_alexa_page_rank() {
        require_once (APPPATH . 'libraries/SEOstats/bootstrap.php');

        //use \SEOstats\Services\Alexa as Alexa;

        try {
            $url = 'https://www.google.com/';

            // Create a new SEOstats instance.
            $seostats = new \SEOstats\SEOstats;

            // Bind the URL to the current SEOstats instance.
            if ($seostats->setUrl($url)) {

                /**
                 *  Print HTML code for the 'daily traffic trend'-graph.
                 */
                echo \SEOstats\Services\Alexa::getTrafficGraph(1);

                /**
                 *  Print HTML code for the 'daily pageviews (percent)'-graph.
                 */
                echo \SEOstats\Services\Alexa::getTrafficGraph(2);

                /**
                 *  Print HTML code for the 'daily pageviews per user'-graph.
                 */
                echo \SEOstats\Services\Alexa::getTrafficGraph(3);

                /**
                 *  Print HTML code for the 'time on site (in minutes)'-graph.
                 */
                echo \SEOstats\Services\Alexa::getTrafficGraph(4);

                /**
                 *  Print HTML code for the 'bounce rate (percent)'-graph.
                 */
                echo \SEOstats\Services\Alexa::getTrafficGraph(5);

                /**
                 *  Print HTML code for the 'search visits'-graph, using
                 *  specific graph dimensions of 320*240 px.
                 */
                echo \SEOstats\Services\Alexa::getTrafficGraph(6, false, 320, 240);
            }
        }
        catch(\Exception $e) {
            echo 'Caught SEOstatsException: ' . $e->getMessage();
        }
    }
}

希望这有帮助

PS:复制应用程序/库文件夹中的SEOstats文件夹