PHP to JSON how to

Hi all,

I was wondering how they do queries with parameters in the url with json/php

I'll explain what I mean...

Normally I know scripts that look like this: ... this will display the json data on the browser


I seen some json url's that look like:

How is this done?

Who is Participating?
Chris StanyonConnect With a Mentor Commented:
A very simple example:

$JSONData = array();

$JSONData['year'] = $_GET['year'];
$JSONData['myVar'] = "Chris";

echo json_encode($JSONData);

Open in new window

Call the script as myScript.php?year=2011 and the output will be:


It will take a value from the URL, and output a JSON object. You could use the GET values to perform database lookups, and then output your JSON object using json_encode -
Usually its a rewrite rule on the server forcing to be handled by a PHP script.. In the PHP script you generate  a json encode in a variable and echo it.

Ray PaseurCommented:
This may be a combination of a rewrite rule and a script that handles a GET-method request.  You might try adding var_dump($_GET) to see what is coming through in the request variables.
error77Author Commented:
Is there anything on the net as an similar example that I can look at? Don't know what I should google for...
Ray PaseurCommented:
Is this question about a jQuery or AJAX activity?  If you can give us a link to a web site that illustrates the question we might be able to offer an explanation of the moving parts.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.