[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

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:

http://mysite.com/myjson.json ... this will display the json data on the browser


I seen some json url's that look like:


How is this done?

1 Solution
Usually its a rewrite rule on the server forcing site.com/api 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...
Chris StanyonCommented:
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 - http://php.net/manual/en/function.json-encode.php
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now