tags:

views:

711

answers:

3

So I want to write a REST API in PHP for JSON for consumption on the iPhone, but also a lot of websites and devices. I have the below code, when accessed via a GET statement, returns a file like:

1mdi2o3.part

How do I return something like: users.json

$db->setQuery( "SELECT * FROM users");
$db->query() or die($queryError);
$numRows = $db->getNumRows();
$row = $db->loadObjectList();

// PRINT JSON FILE
header("Content-type: application/json");

for($i = 0 ; $i < $numRows; $i++){
$json_output[$i] = $row[$i];
}

$MYjson_output = json_encode($json_output);

echo $MYjson_output;
A: 

Hi Brian,

Welcome to SO. Have you considered using the Zend framework for this application? I've written about this topic before, and if you do use Zend I could probably be of additional help. It would certainly help get you past the basics.

HTH,

-aj

AJ
Sorry, but "use Zend" seems like a bit of overkill for solving a simple problem like this...
Wim
@Wim, the OP appears to be developing his own REST server and I was merely pointing out an alternative to reinventing the wheel. BTW, see my comment on *your* answer.
AJ
You're right, it can be useful for him. But if it can be solved in 10 lines, I'm always hesitant to add in some 10K lines of framework first ;-)
Wim
A: 

The problem is this line:

header("Content-type: application/json");

I know this looks like the right way to do (after all, application/json is the official MIME type for JSON content), but it causes most browsers to present you with a file download dialog, when you just want to see the text. You can use the text/plain encoding to avoid this. Note that your AJAX/iPhone/... app probably doesn't care about the content type, so your API will work in both cases.

Also see this blog post which provides some more context.

Wim
@Wim, wouldn't it be text/json ?
AJ
I was just quoting the OP here. `application/json` is the official one though I think. Maybe with `text/json` you could fool the browser into displaying it as plain text, but in that case `text/plain` would be equivalent I guess.
Wim
According to http://stackoverflow.com/questions/477816/the-right-json-content-type `application/json` is the best option.
Kitson
Also, I use `text/json` and don't have a problem on IE6-8, FF3 and Chrome, but I don't know about WebKit stuff like the iPhone.
Kitson
This was exactly what I was looking for, simple. Thanks.
Brian H
You should really use the proper MIME type and text/plain is not it. I use Firebug in Firefox to test my JSON web services and use console.log to show me the JSON data. It is a great way to verify it is working properly. You will find that some systems want to the proper MIME type or they will reject the data.
Brennan
+2  A: 

Not entirely such what your goal is but here are 3-4 solutions that might work:

Common Solutions

Rewrite

This is probaly the most conventional way of getting clean URIs for your API. If you want the user part of user.json to be dynamic, you can use mod_rewrite (again assuming Apache) to rewrite your URLs to a php handler script. If you are going the conventional RESTful style URL route, you will probably want to use this anyway to achieve clean / separated URLs.

# For grabbing all users
RewriteEngine on
RewriteRule ^users\.json rest.php [L]

# For grabbing one particular user
RewriteEngine on
RewriteRule ^([a-z0-9]+)\.json rest.php?user=$1 [L]

Where rest.php is your PHP handler script.

URL Rewriting without mod-rewrite

If you don't want to use mod_rewrite, you can also do something like

example.com/rest.php/users.json
example.com/rest.php/user-id.json

or even

example.com/rest.php/user-id
example.com/rest.php/user/user-id

Where rest.php is your PHP handler script. You can grab the user-id from the URL (or URI if we're talking RESTful terms) using the $_SERVER global with $_SERVER['REQUEST_URI'].

Other solutions

Changing download name via Content-Disposition:

I believe you want to add the Content-Disposition header...

<?php
    header('Content-Disposition: attachment; filename="users.json"');
?>

This will force the file to be downloaded as user.json. This usually isn't the behavior expected for a REST API, but from the way your question was worded, I figured I'd throw it out there.

AddHandler (or AddType)

Assuming your running an Apache server, you can also just use AddHandler directive to make .json extension files be treated like they are php.

AddHandler application/x-httpd-php .json

Warning: Other plain .json files will be treated as PHP so you'd probably want to set this in a .htaccess file in the directory with the PHP script. This would work fine for this one URI but not ideal if you were exposing many URIs

nategood