简体   繁体   English

CodeIgniter RESTful API库响应

[英]CodeIgniter RESTful API Library Response

This question is related to the CodeIGniter RESTful API library found here. 该问题与此处找到的CodeIGniter RESTful API库有关。 I hope someone here is using this library and can offer some help :) . 我希望这里的人正在使用此库,并可以提供一些帮助:)。

http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/ http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/

I have installed this library and set up a working environment with it. 我已经安装了该库并为其建立了工作环境。 I have been able to get data back as well. 我也能够取回数据。 For use, I have created a very simple class that I am accessing with the following code: 为了使用,我创建了一个非常简单的类,可以使用以下代码进行访问:

<?php

 require APPPATH.'/libraries/REST_Controller.php';

 class Users extends REST_Controller
 {
      public function list_get()
      {
           $this->load->database();

           $data = $this->db->get('users')->result();

           $this->response($data, 200); 
       }

  }

To get at this controller, I have done a call to the following URL: 为了使用此控制器,我已经调用了以下URL:

"http://localhost/mgtapp/index.php/api/users/list/format/json" “ http://localhost/mgtapp/index.php/api/users/list/format/json”

While I am getting back data, I see that the header content type is set to text/html instead of json and I am also getting errors in php that say "headers already sent". 当我取回数据时,我看到标头内容类型设置为text / html而不是json,而且我在php中也收到错误消息,提示“标头已发送”。 I have tried to remove the format from the end of the url and send it in via an "accept" but I get the same errors and I see the content type being set as text/html. 我尝试从url末尾删除格式,并通过“接受”发送,但是出现相同的错误,并且看到内容类型设置为text / html。 When I run the example, I see the response coming back as it should (as json in the content type) so I do not understand what I am doing wrong here that the content type is not being set correctly. 当我运行示例时,我看到响应按原样应返回(内容类型为json),所以我不明白我在做什么,因为内容类型设置不正确。 IF someone can shed some light it would be super helpful! 如果有人可以说出一些话,那将非常有帮助!

Thanks! 谢谢!

So I am answering this question because the reason for my troubles was a stupid one. 我之所以回答这个问题,是因为麻烦的原因是愚蠢的。

I had a couple of lines under the "?>" of my php class controller and apparently that was messing it up. 我在php类控制器的“?>”下有几行,显然是搞砸了。 Sorry! 抱歉!

Anybody else with this issue, check that first! 遇到此问题的其他人,请先检查!

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

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