i following video tutorial restful webservice (creating restful web service in php). in tutorial, can pass name of book via url retrieve price.
here code :
index.php
<?php // process client request via url header("content-type:application/json"); include("functions.php"); if(!empty($_get['name'])) { // if have name of book in our url $name=$_get['name']; $price=get_price($name); if(empty($price)) // book not found deliver_response(200,"book not found", null); else // response book price deliver_response(200,"book_found", $price); } else { // throw invalid request deliver_response(400,"invalide request", null); } function deliver_response($status, $status_message, $data) { header("http/1.1 $status $status_message"); $response['status']=$status; $response['status_message']=$status_message; $response['data']=$data; $json_response=json_encode($response); echo $json_response; } ?>
function.php
<?php function get_price($find) { $books=array ( "java"=>299, "c"=>348, "php"=>267 ); foreach($books $book=>$price) { if($book=$find) { return $price; break; } } } ?>
.htaccess
# turn on rewrite engine options +followsymlinks rewriteengine on # request routing rewriterule ^([a-za-z_-]*)$ index.php?name=$1 [nc, qsa]
while try tutorial without .htaccess file, works well. while put file directory, crash "server error", 500 error.
can explain me please ?
Comments
Post a Comment