所以我尝试使用以下文档与https://www.gosquared.com api进行交互:
https://www.gosquared.com/developer/api/now/v3/aggregateStats/
现在我在REST
和CURL
使用codeigniter我有以下代码:
public function __construct()
{
parent::__construct();
$this->load->library('admin/curl');
$this->load->library('admin/rest');
$this->rest->initialize(array(
'server' => 'https://api.gosquared.com/now/v3',
));
$this->rest->api_key('0000000000000000');
$this->rest->site_token('FFF-000000-F');
}
public function index()
{
$result = $this->rest->get('aggregateStats');
var_dump($result);
}
因此,这与服务应该进行交互,除了在api密钥之外,此服务还需要site_token
才能访问我的信息。
使用我现在的代码$this->rest->site_token('FFF-000000-F');
我在加载页面时遇到了这个特定的错误:
Fatal error: Call to undefined method REST::site_token()
如果我删除导致该错误的代码行,我会因var_dump($result);
而得到此结果(因为网站令牌未被传递!)
object(stdClass)#27 (2) { ["code"]=> int(4000) ["message"]=> string(162) "You must specify a site token. It looks like GSN-1234567-X and can be found in the developer section of your account area https://www.gosquared.com/home/developer" }
所以我的问题是:
如何在codeigniter中将额外变量site_token
传递到rest library
而不会收到任何错误?
根据文档,与api交互的有效URL看起来如下(为了它的价值)
https://api.gosquared.com/now/v3/aggregateStats?api_key=0000000000000000&site_token=FFF-000000-F
答案 0 :(得分:0)
通过将api密钥和site_key放入实际请求中的数组中,我能够使其工作。
// Removed these lines of code:
$this->rest->api_key('0000000000000000');
$this->rest->site_token('FFF-000000-F');
// Replaced the $result = $this->rest->get('aggregateStats'); with the following:
$data['result'] = $this->rest->get('aggregateStats', array('api_key' => '0000000000000000','site_token' => 'FFF-000000-F'));
只是附注,可以考虑将api密钥和站点令牌放入配置文件中,以便将来更容易更新。