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

x
  • 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

now...

I seen some json url's that look like:

http://mysite.com/api/get_cars?year=2000

How is this done?

Thanks
0
error77
Asked:
error77
1 Solution
 
p_nutsCommented:
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.

Easy..
0
 
Ray PaseurCommented:
http://mysite.com/api/get_cars?year=2000
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.
0
 
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...
0
 
Chris StanyonCommented:
A very simple example:

<?php
$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:

{"year":"2011","myVar":"Chris"}

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
0
 
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.
0

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