1

I'm trying to create an API.

I have a controller that does something, and returns let's say "ABC" as a string. my url looks like this:

     http://myserver/myapp/myAPImethod/parm1

inside the method, I have code like this:

    header ('Content-Type: application/json; charset=UTF-8');
    $model= str_replace("%snv%"," ",$model);
    echo json_encode($model);

The application that consumes this API has a controller that does the following:

public function curl($url){
  //echo 'in the routine';
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data=curl_exec($ch);
    print_r($data);
    curl_close($ch);
    return $data;
  }
    $url = "http://myserver/myapp/index.php/myAPImethod/hname";
    $jsondata = $this->curl($url);      
    //print_r(json_decode($jsondata));

My question is this: When I run the application that consumes my API, because I have an echo statement, it shows "ABC" on the page. But I don't want it to display the results. I just want it to pass the data to the consuming application. I've tried changing the echo statement in the API method to a "return" statement, but I don't get any data sent over to the caller.

Thanks.

Jordan Arseno
  • 6,487
  • 7
  • 48
  • 94
dot
  • 11,828
  • 34
  • 79
  • 156
  • 1
    Why are you trying to return something from a method in a CodeIgniter controller? If you want to display the data, you should pass it through to a view. If you want to store it, you should create a method in one of your models to put it in the database. I don't see any reason to ever return anything. – Matthew Daly Aug 17 '12 at 19:41
  • 2
    @mattbd: dot is building an API. It's perfectly logical, APIs don't have views. The OP is also not asking about storage. – Jordan Arseno Aug 17 '12 at 19:43
  • 1
    I'd guess it's actually the `print_r($data)` in your `curl` method that's causing the API response to appear on your page. – Richard M Aug 17 '12 at 19:47
  • 1
    I've been in this exact situation before and the way I solved it was to digitally sign the request and only serve content to clients (consuming servers) that the server knew about. It worked fine, but required that the server and client (consuming server) exchange information beforehand (I was in control of both). Curious what people come up with here. I removed the CodeIgniter tag -- might get more traffic -- it's more of a general API + PHP question. – Jordan Arseno Aug 17 '12 at 19:49
  • @Richard M - that was it- oversight on my part. – dot Aug 17 '12 at 19:51
  • @JordanArseno Ah, I see. Technically, the curl method shouldn't really be in the controller - it would make more sense to put it in a separate helper, but you're right, this is more of a general PHP & API question – Matthew Daly Aug 17 '12 at 19:56
  • @mattbd - you mean i should create a separate class and then load it in my controller's constructor? – dot Aug 17 '12 at 19:57
  • @dot No - in CodeIgniter you can create your own helpers in addition to those that ship with CodeIgniter. These are essentially just collections of functions. For instance, I wrote one a while back to validate the IPN response from PayPal. It's basically just a handy way of creating a function for use in your application. I found http://stackoverflow.com/questions/804399/codeigniter-create-new-helper very useful in building my first helper, particularly the first answer – Matthew Daly Aug 17 '12 at 20:06

1 Answers1

0

Create a JSON view and pass data to the json view

// In view create a file json_view.php and paste this content

// RFC4627-compliant header
header('Content-type: application/json');

// Encode data
if(isset($response)) {
    echo json_encode($response);
}
else
    echo json_encode(array('error' => true));

And in your controller

// Build our view's data object
$data = array('response' => $response_data);
// Load the JSON view
$this->load->view('json_view', $data);
vimal1083
  • 7,394
  • 6
  • 30
  • 50