问题描述
您好我要将与codeigniter中的项目集成,是否有人为我提供解决方案?
我试图使SEOstats类作为帮助器,并加载在特定控制器中的帮助器,但空白页显示,我也尝试通过视图但我看到的同一个空白页,
我已经在我的视图文件中包含这个代码,SEOstats目录也在相同的views目录。
<?php
require_once'SEOstats / bootstrap.php';
使用\SEOstats\Services作为SEOstats;
try {
$ url ='http://www.google.com/';
//创建一个新的SEOstats实例。
$ seostats = new \SEOstats\SEOstats;
//将URL绑定到当前的SEOstats实例。
if($ seostats-> setUrl($ url)){
echo SEOstats\Alexa :: getGlobalRank();
echo SEOstats\Google :: getPageRank();
}
}
catch(SEOstatsException $ e){
die($ e-> getMessage());
}
我也用它作为库
<?php
命名空间SEOstats;
使用SEOstats\Common\SEOstatsException作为E;
使用SEOstats\Config as Config;
使用SEOstats \Helper作为助手;
使用SEOstats \Services作为服务;
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;
}
/ **
*确保URL设置,否则返回默认值
* @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 =新\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;
}
}
p>
$ this-> load-> library('SEOstats')
但我最近还在寻找一个解决方案,最后写了我自己的,我想把它留在这里,以防任何人在寻找一个解决方案在未来。
if(!defined('BASEPATH'))
exit('不允许直接脚本访问');
class SEOstatistics {
private $ seostats;
function __construct(){
require_once(APPPATH。'third_party / seostats / bootstrap.php');
$ this-> seostats = new \SEOstats\SEOstats;
}
私有函数alexa(){
return new \SEOstats\Services\Alexa;
}
私人函数google(){
return new \SEOstats\Services\Google;
}
私人函数moz(){
return new \SEOstats\Services\Mozscape();
}
私人函数openSiteExplorer(){
return new \SEOstats\Services\OpenSiteExplorer();
}
私有函数semRush(){
return new \SEOstats\Services\SemRush();
}
私有函数sistrix(){
return new \SEOstats\Services\Sistrix();
}
私有函数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();
Hello I want to integrate the SEOStats Class with a project in codeigniter , is anyone provide me solution ?
I have tried to make the SEOstats class as a helper and load the helper in the specific controler , but a blank page is showing , I also try to include it via view but the same blank page i am seeing ,
I have included this code in my view file , the SEOstats directory also in the same views directory .
<?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());
}
i have also used it as library
<?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;
}
}
and loaded the library as
$this->load->library('SEOstats');
I know this post is old. But I was looking for a solution as well recently and ended up writing my own and figured I would leave it here in case anyone else was looking for a solution in the future.
Place the following in a library file and autoload if you want.
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;
}
}
}
And then an example of using it in a controller or model is:
$google = $this->seostatistics->google($url);
$rank = $google->getPageRank();
这篇关于如何集成SEOStats与Codeigniter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!