繁体   English   中英

PHP将数据发布到API页面

[英]PHP Post data to API page

我有一个用于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;

您可以在初始化数组后将其强制转换为object

return (object) array('status' => 'test', 'reason' => 'test2');

https://3v4l.org/dKWAT

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM