PHP to JSON how to

Posted on 2012-08-31
Last Modified: 2012-09-01
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?

Question by:error77
    LVL 13

    Expert Comment

    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.

    LVL 107

    Expert Comment

    by:Ray Paseur
    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.

    Author Comment

    Is there anything on the net as an similar example that I can look at? Don't know what I should google for...
    LVL 42

    Accepted Solution

    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 -
    LVL 107

    Expert Comment

    by:Ray Paseur
    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now