简体   繁体   中英

Unable to get json data from rest api in php

code:

<?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 controller

<?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);
    }
}

output:

array(0) { }

eventapi data

[{"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"}]

I am creating a simple rest API using Codeigniter where API works fine. Now, the problem is that when I am getting data from eventapi using curl it shows me output ie array(0) { } I don't know why? So, How can I solve this issue? Please help me.

Thank You

If the above one is your code, you didn't add any parameters. The array passing with empty input. So you get empty output. Please correct the input. curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $inputParameters");

Please add these lines along with the curl .

And don't forget to add the header also . //set the content type to application/json

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

//return response instead of outputting curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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