我想在我的网站上创建一个页面,其中包含来自 API 的一些数据。该网站是用 Wordpress 制作的。
我尝试过在网上找到的各种代码/功能,但没有成功。
API 要求我按用户、密码或令牌登录(我更喜欢令牌)。
我是 API 和 PHP 的新手,我什至不知道从哪里开始。
如果之前有人使用过它,我想从 Exoclick API 获取数据。
从 Exoclick 上的 API 接口我可以看到请求是用 curl 发出的,我以前从未使用过它,我也不想使用它。这是一个例子
curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer tokenhere' 'https://api.exoclick.com/v2/statistics/publisher/country?'
从API手册中,我找到了这个登录代码,但是将它放在一个随机页面进行测试,它只会使我的网站崩溃:
<?php
// Include Request and Response classes
$url = 'https://api.exoclick.com/v2/login';
$params = array(
'api_token' => 'tokenhere'
);
// Create a new Request object
$request = new Request($url, 'POST', $params);
// Send the request
$request->send();
// Get the Response object
$response = $request->getResponse();
if($response->getStatusCode() == 200) {
// Retrieve the session token details
$token = $response->getBodyDecoded();
print_r($token);
}
else {
echo $response->getStatusCode() . PHP_EOL;
echo $response->getReasonPhrase() . PHP_EOL;
echo $response->getBody() . PHP_EOL;
}
?>