我有一个API的PHP类:
class Integra {
public function __construct()
{
$this->api_key = 'API-XXXXXXX';
$this->api_salt = '';
}
public function build_query($type, $data = array())
{
//$salted = hash_hmac('md5', $this->api_key, $this->api_salt);
//$data['api_key'] = $salted;
//$data['key'] = $this->api_key;
$params = $data;
$data_string = json_encode($params);
$ch = curl_init('http://domain.com/api.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
return $response;
}
}
所以这是将数据发布到相关页面/功能。
我希望能够将数据作为对象返回,我在发布数据的页面上尝试了以下内容,但没有返回任何内容。
我做错了什么?
<?php
return array('status' => 'test', 'reason' => 'test2');
?>
以下是我如何调用类和函数:
$Integra = new Integra();
$output = $Integra->build_query('check_system.php');
echo $output->reason;
echo $output->status;
答案 0 :(得分:1)
初始化数组后,您可以将其强制转换为object
。
return (object) array('status' => 'test', 'reason' => 'test2');