【问题标题】:Unable to get json data from rest api in php无法从php中的rest api获取json数据
【发布时间】:2019-07-23 09:00:52
【问题描述】:

代码:

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

事件API数据

[{"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 创建一个简单的休息 API,其中 API 工作正常。现在,问题是当我使用curleventapi 获取数据时,它会显示输出,即array(0) { } 我不知道为什么?那么,我该如何解决这个问题?请帮帮我。

谢谢

【问题讨论】:

  • eventapi是控制器功能吗?如果是,分享eventapi 的代码
  • 检查@VinayPatil
  • 您检查过查询吗?是返回正确的数据吗?
  • 另外,检查$client_id的值,如果它是未定义的,那么它将导致查询中的数据为空
  • 是的,我检查并返回正确的数据@VinayPatil

标签: php jquery json codeigniter


【解决方案1】:

如果上面是你的代码,你没有添加任何参数。以空输入传递的数组。所以你得到空输出。请更正输入。 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $inputParameters");

请将这些行与 curl 一起添加。

并且不要忘记添加标题。 //设置内容类型为application/json

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

//返回响应而不是输出 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

【讨论】:

    猜你喜欢
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2015-11-17
    相关资源
    最近更新 更多