我是codeigniter的新手,我正在尝试将Klout API集成到codeigniter,我有这个错误说试图获取非对象的属性这里
我已将API放在应用程序文件夹的一侧
我将成为KloutAPIv2.class
<?php
// Define
DEFINE("HTTP_GET","GET");
DEFINE("HTTP_POST","POST");
// Get Started
class KloutAPIv2 {
/** @var String $BaseUrl The base url for the Klout API */
private $BaseUrl = "http://api.klout.com/";
/** @var String $Version YYYYMMDD */
private $Version = '20120511';
/** @var String $KloutKey */
private $KloutKey;
/**
* Constructor for the API
* Prepares the request URL and client api params
* @param String $client_id
* @param String $client_secret
* @param String $version Defaults to v2, appends into the API url
*/
public function __construct($kloutapi_key = false, $version="v2"){
$this->BaseUrl = "{$this->BaseUrl}$version/";
$this->KloutKey = $kloutapi_key;
}
/******************************************************/
// Identity
/******************************************************/
/**
* KloutIDLookupByName
* Looks up a Klout ID from a Screen Name
* @param String $network The network to look the screen name up on
* @param String $screenName The screen name to look up
*/
public function KloutIDLookupByName($network, $screenname){
// Build the URL
$url = $this->BaseUrl . "identity.json/". $network;
// Append the lookup details
$params['screenName'] = $screenname;
$params['key'] = $this->KloutKey;
// Return the result;
$CurlResult = $this->GET($url,$params);
$ResultString = json_decode($CurlResult);
// Assume it only returns "ks" data:
$KloutID = $ResultString->id;
return $KloutID;
}
/**
* KloutIDLookupByID
* Looks up a Klout ID from a Twitter ID
* @param String $network The network to look the screen name up on
* @param String $id The screen name to look up
*/
public function KloutIDLookupByID($network, $id){
// Build the URL
$url = $this->BaseUrl . "identity.json/". $network ."/". $id;
// Append the lookup details
$params['key'] = $this->KloutKey;
// Return the result;
$CurlResult = $this->GET($url,$params);
$ResultString = json_decode($CurlResult);
// Assume it only returns "ks" data:
$KloutID = $ResultString->id;
return $KloutID;
}
/**
* KloutIDLookupReverse
* Looks up a Klout ID from a Twitter ID
* @param String $network The network to look the screen name up on
* @param String $id The screen name to look up
*/
public function KloutIDLookupReverse($network, $id){
// Build the URL
$url = $this->BaseUrl . "identity.json/klout/". $id ."/". $network;
// Append the lookup details
$params['key'] = $this->KloutKey;
// Return the result;
return $this->GET($url,$params);
}
/******************************************************/
// User
/******************************************************/
/**
* KloutUser
* Looks up Klout User Data
* @param String $id The Klout ID to look up
*/
public function KloutUser($id){
// Build the URL
$url = $this->BaseUrl ."user.json/". $id;
// Append the lookup details
$params['key'] = $this->KloutKey;
// Return the result;
return $this->GET($url,$params);
}
/**
* KloutUserScore
* Looks up Klout User Score
* @param String $id The Klout ID to look up
*/
public function KloutUserScore($id){
// Build the URL
$url = $this->BaseUrl ."user.json/". $id ."/score";
// Append the lookup details
$params['key'] = $this->KloutKey;
// Return the result;
return $this->GET($url,$params);
}
/**
* KloutUserTopics
* Looks up Klout User Topics
* @param String $id The Klout ID to look up
*/
public function KloutUserTopics($id){
// Build the URL
$url = $this->BaseUrl ."user.json/". $id ."/topics";
// Append the lookup details
$params['key'] = $this->KloutKey;
// Return the result;
return $this->GET($url,$params);
}
/**
* KloutUserInfluence
* Looks up Klout User Influence
* @param String $id The Klout ID to look up
*/
public function KloutUserInfluence($id){
// Build the URL
$url = $this->BaseUrl ."user.json/". $id ."/influence";
// Append the lookup details
$params['key'] = $this->KloutKey;
// Return the result;
return $this->GET($url,$params);
}
/******************************************************/
// Scores
/******************************************************/
/**
* KloutScore
* Looks up Klout Score, does not return any other data
* @param String $id The Klout ID to look up
*/
public function KloutScore($id){
// Use the Klout Score Data call to pull just the Score
$CurlResult = $this->KloutUserScore($id);
$ResultString = json_decode($CurlResult);
$KloutScore = $ResultString->score;
return $KloutScore;
}
/**
* KloutScore
* Returns changes in klout score
* @param String $id The Klout ID to look up
* @param String $period options are either day, week, month
*/
public function KloutScoreChanges($id, $period){
// Use the Klout Score Data call to pull just the Score
$CurlResult = $this->KloutUserScore($id);
$ResultString = json_decode($CurlResult);
if ($period == "day") {
$KloutScoreChanges = $ResultString->scoreDelta->dayChange;
} elseif ($period == "week") {
$KloutScoreChanges = $ResultString->scoreDelta->weekChange;
} else {
$KloutScoreChanges = $ResultString->scoreDelta->monthChange;
}
return $KloutScoreChanges;
}
/******************************************************/
// Utility
/******************************************************/
/**
* Request
* Performs a cUrl request with a url generated by MakeUrl.
* @param String $url The base url to query
* @param Array $params The parameters to pass to the request
*/
private function Request($url,$params=false,$type=HTTP_GET){
// Populate data for the GET request
if($type == HTTP_GET) $url = $this->MakeUrl($url,$params);
// borrowed from Andy Langton: http://andylangton.co.uk/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
if ( isset($_SERVER['HTTP_USER_AGENT']) ) {
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
}else {
// Handle the useragent like we are Google Chrome
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.X.Y.Z Safari/525.13.');
}
curl_setopt($ch , CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Populate the data for POST
if($type == HTTP_POST){
curl_setopt($ch, CURLOPT_POST, 1);
if($params) curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
$result=curl_exec($ch);
$info=curl_getinfo($ch);
curl_close($ch);
return $result;
}
/**
* GET
* Abstraction of the GET request
*/
private function GET($url,$params=false){
return $this->Request($url,$params,HTTP_GET);
}
/**
* POST
* Abstraction of a POST request
*/
private function POST($url,$params=false){
return $this->Request($url,$params,HTTP_POST);
}
/**
* MakeUrl
* Takes a base url and an array of parameters and sanitizes the data, then creates a complete
* url with each parameter as a GET parameter in the URL (Credit to Stephen Young)
* @param String $url The base URL to append the query string to (without any query data)
* @param Array $params The parameters to pass to the URL
*/
private function MakeUrl($url,$params){
if(!empty($params) && $params){
foreach($params as $k=>$v) $kv[] = "$k=$v";
$url_params = str_replace(" ","+",implode('&',$kv));
$url = trim($url) . '?' . $url_params;
}
return $url;
}
} ?&GT;
我将控制器创建为klout.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class klout extends CI_Controller {
public $kloutapi_key;
function __construct() {
parent::__construct();
$this->load->file('./klout/KloutAPIv2.class.php');
$this->kloutapi_key = 'key';
}
public function kloutID() {
$network = "twitter";
$userid = 318066771330736;
$klout = new KloutAPIv2($this->kloutapi_key);
$kloutid = $klout->KloutIDLookupByID($network, $userid);
print_r($kloutid);
exit();
}
}
?>
答案 0 :(得分:1)
适应Twitter ID与Twitter屏幕名称之间略有不同:URL结构不同,“网络”字符串不同:
[这主要是我们API Proxy的路由匹配系统的解决方法]。
所以,按ID: http://api.klout.com/v2/identity.json/tw/318066771330736?key=foo
按屏幕名称: http://api.klout.com/v2/identity.json/twitter?screenName=jack&key=foo
答案 1 :(得分:0)
我认为您必须将代码作为库加载,而不是使用$this->load->file
,我查看有关$this->load->file
的Code Igniter用户指南:
$ this-&gt; load-&gt;文件('filepath / filename',true / false)
这是一个通用的文件加载功能。提供文件路径和名称 在第一个参数中,它将打开并读取文件。默认情况下 数据将被发送到您的浏览器,就像查看文件一样,但如果您 将第二个参数设置为true(布尔值),它将返回 数据为字符串。
所以,我的推荐是使用库。将您的KloutAPIv2.class.php
放入应用程序/库文件夹中名为klout.php
的文件中,然后加载$this->load->library('kout');
您可以在加载库时将参数传递给构造函数,如下例所示:
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('Someclass', $params);
更多信息:http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html