1

我是codeigniter的新手,我正在尝试将Klout API集成到codeigniter,我有这个错误说Trying to get property of non-object Here

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

} ?>

我创建了一个控制器作为 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();

    }

}

?>
4

2 回答 2

1

Twitter ID 与 Twitter 屏幕名称的适应略有不同:URL 结构不同,“network”字符串也不同:

  • ID呼叫时,网络为“tw”
  • 用网名打电话时,网络是“twitter”

[这主要是我们 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

于 2012-12-03T17:23:22.040 回答
0

我认为您必须将代码作为库加载,而不是使用$this->load->file,我查看 Code Igniter 用户指南$this->load->file

$this->load->file('文件路径/文件名', true/false)

这是一个通用的文件加载功能。在第一个参数中提供文件路径和名称,它将打开并读取文件。默认情况下,数据会发送到您的浏览器,就像查看文件一样,但是如果您将第二个参数设置为 true(布尔值),它会将数据作为字符串返回。

所以,我的建议是使用图书馆。将您KloutAPIv2.class.php的文件放入klout.phpapplication/libraries 文件夹中并加载它$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

于 2012-12-03T13:03:36.233 回答