我无法使用CURL进行API调用。下面是使用CURL 进行API调用的代码
$ch=curl_init("http://sms.geekapplications.com/api/balance.php?authkey=2011AQTvWQjrcB56d9b03d&type=4");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Authorization: Bearer"));
// execute the api call
$result = curl_exec($ch);
echo ($result);
首先,您可能想要使用一个函数来实现这一点。。并且你的CURL没有正确构建。请参阅我的示例
//gets geekapplications SMS balance
function getBalance() {
$url = 'http://sms.geekapplications.com/api/balance.php?' . http_build_query([
'authkey' => '2011AQTvWQjrcB56d9b03d',
'type' => '4'
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http == 200) {
$json = @json_decode($response, TRUE);
return $json;
} else {
echo 'There was a problem fetching your balance...';
}
}
在你的控制器中使用它,尝试print_r($this->getBalance());
应该输出一个带有你平衡的数组。