[英]Unable to get json data from rest api in php
碼:
<?php
$url = base_url()."eventapi";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
?>
eventapi控制器
<?php
require APPPATH . '/libraries/REST_Controller.php';
use Restserver\Libraries\REST_Controller;
class Eventapi extends REST_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function index_get()
{
$data['client_id'] = $this->session->userdata('client_id');
$client_id = $data['client_id'][0]['client_id'];
$this->db->select('*');
$this->db->from('event');
$this->db->where('client_id',$client_id);
$sql = $this->db->get();
$result = $sql->result_array();
$this->response($result, 200);
}
}
輸出:
array(0) { }
eventapi數據
[{"id":"1","client_id":"20190702082406","event_type":"Private Event","event_name":"Birthday Celebration","event_date":"2019-08-15","event_time":"08:00 PM","event_des":"Birthday celebration ","s_date":"2019"}]
我正在使用Codeigniter創建一個簡單的Rest API,其中API可以正常工作。 現在的問題是,當我使用curl
從eventapi
獲取數據時,會顯示輸出,即array(0) { }
我不知道為什么? 那么,我該如何解決這個問題? 請幫我。
謝謝
如果上面的代碼是您的代碼,則無需添加任何參數。 傳遞空輸入的數組。 這樣您就得到了空的輸出。 請更正輸入。 curl_setopt($ ch,CURLOPT_POST,1); curl_setopt($ ch,CURLOPT_POSTFIELDS,$ inputParameters“);
請在卷曲的同時添加這些線條。
並且不要忘記添加標頭。 //將內容類型設置為application / json
curl_setopt($ ch,CURLOPT_HTTPHEADER,array('Content-Type:application / json'));
//返回響應,而不是輸出curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.