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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

PHP REST API Intro

I am going to be working on a api that use only using GET requests
i would like the parameters to be within the url statement rather than url parameters so

www.site.com/json/movie/en/US

rather than www.site.com/json/index.php?part=movie&language=en&country = US

My question is how do i get something like www.site.com/json/movie/en/US to actually run a script or php page.  Im used to running say mypage.php and adding url parameters on to it.  How can www.site.com/json/movie/en/US launch mypage.php then how do i grab the resources out of the url so i have vars such as $country set up with US using my script.
0
Brant Snow
Asked:
Brant Snow
3 Solutions
 
Brant SnowAuthor Commented:
So do i have to write multiply mod rewrites because well i dont know if they are going to hit

www.site.com/json/movie/en/US  or maybe

www.site.com/json/movie/es/CA , so if that was the case would i have to include  a mod rewrite for every possibility?
0
 
hieloCommented:
No. You can write a rule so that if the url starts with "/json/" then have it redirect to site.com/restAPI.php?params=XXX where XXX is what follows /json/. Your rest.API should be able to "pick up"/see $_GET['params']
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ray PaseurCommented:
Hey, @thawts, there is exactly zero advantage to making a rewrite rule for an API.  Just  do things the way that Yahoo and Google do it -- add the URL GET-method parameters to the end of the URL and it is easy.  Example here:
http://www.laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Ray&resp=XML


<?php // RAY_REST_get_last_name.php
error_reporting(E_ALL);



// DEMONSTRATE HOW A RESTFUL WEB SERVICE WORKS
// INPUT FIRST NAME, OUTPUT FAMILY NAME
// CALLING EXAMPLE:
// file_get_contents('http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&resp=XML&name=Ray');



// OUR DATA MODEL CONTAINS ALL THE ANSWERS - THIS COULD BE A DATA BASE - AS SIMPLE OR COMPLEX AS NEEDED
$dataModel
= array
( 'Brian'   => 'Portlock'
, 'Ray'     => 'Paseur'
, 'Richard' => 'Quadling'
, 'Dave'    => 'Baldwin'
)
;


// RESPONSE CAN BE PLAIN TEXT OR XML FORMAT
$alpha = NULL;
$omega = NULL;
if ( (isset($_GET["resp"])) && ($_GET["resp"] == 'XML') )
{
    $alpha = '<response>';
    $omega = '</response>';
}



// TEST THE API KEY
$key = FALSE;
if (isset($_GET["key"])) $key = $_GET["key"];
if ($key !== 'ABC') die($alpha . 'BOGUS API KEY' . $omega);



// LOOK UP THE FAMILY NAME
$name="?";
if (isset($_GET["name"])) $name = $_GET["name"];

// IF THE URL NAME IS FOUND IN THE DATA MODEL
if (array_key_exists($name, $dataModel))
{
    // RETURNS THE APPROPRIATE FAMILY NAME FROM THE DATA MODEL
    die($alpha . "$dataModel[$name]" . $omega);
}

// RETURNS THE UNKNOWN NAME INDICATOR
else die($alpha . 'UNKNOWN' . $omega);

Open in new window

0
 
crazedsanityCommented:
If you have a script, "/json.php", you can use a URL like "/json.php/movie/en/US".  The trailing "/movie/en/US" part is just an argument.  The trick, if you want to have that first part be "/json" (without ".php"), is to create a file called "json" in the same place that the "json.php" file would be, and add this to your .htaccess file:
<files json>
        ForceType application/x-httpd-php
</files>

Open in new window


Apache determines how a file should be parsed based on it's extension; since the file doesn't have an extension, you have to tell Apache how it should be parsed.
0
 
crazedsanityCommented:
Just a thought: it might not necessarily fit your situation, but I built a PHP-based framework that fills this need (it can be used for the whole site or just one script): [ https://cs-content.svn.sourceforge.net/svnroot/cs-content/releases/1.0/ ]

If you don't use SVN (Subversion), you can download the last zip from http://sourceforge.net/projects/cs-content/ (v1.0 isn't available there yet, apparently).
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