4

您好,我想将SEOStats 类与 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');
4

2 回答 2

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();
于 2016-02-21T13:18:07.683 回答
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 文件夹复制到 application/libraries 文件夹中

于 2014-05-30T18:48:25.060 回答