Brian Pace
Brian Pace

Reputation: 155

PHP API not working in REST implementation

I am trying to build a simple RESTful API using PHP. The request works with a normal query in the URL path, however, I am getting a "404 URL not found" error when implementing REST approach

.htaccess

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([a-zA-Z|_-]*)$ index.php?name=$1 [nc,qsa]

index.php (snippet)

// process client request (via URL)
header("Content-Type:application/json");
include('functions.php');
if(!empty($_GET['name'])){

    $name=$_GET['name'];
    $result=get_items_by_category("$name");

    if(empty($result)){
      print($name);
      deliver_response(200,"Duh, gee, Mort, there's nothing here!",NULL);

      }else{
      deliver_response(200,"Hit the road,Jack!",$result);
      }

  }else{

  deliver_response(400,"This is not the request you're looking for",NULL);

}

Upvotes: 1

Views: 550

Answers (1)

Ben Green
Ben Green

Reputation: 438

Quote:

I did some more debugging, and I found out that /api/Grill(my) does not work, 
maybe due to parentheses in the name? Because /api/Pasta works

There are no parentheses in the regex so yeah it won't match that.

What is your plan for URL syntax? can help you with a regex to get it to work.

Upvotes: 1

Related Questions