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

我曾尝试将 SEOstats 类作为助手并在特定 Controller 中加载助手,但显示了一个空白页面,我也尝试通过 View 包含它,但我看到的是相同的空白页面,

我已经将这段代码包含在我的 View 文件中,SEOstats 目录也在同一个 View 目录中。

    <?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');

最佳答案

我知道这个帖子很旧。但我最近也在寻找解决方案,最终自己写了一个解决方案,并认为我会把它留在这里,以防其他人在 future 寻找解决方案。

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

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;
       }
   }
}

然后在 Controller 或模型中使用它的一个例子是:
$google = $this->seostatistics->google($url);
$rank = $google->getPageRank();

关于php - 如何将 SEOStats 与 Codeigniter 集成?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19726314/

10-12 13:38