我正在关注有关RESTful Web服务(Creating a RESTful Web Service in PHP)的视频教程。 在本教程中,我们可以通过URL传递书籍名称来检索价格。
以下是代码:
的index.php
<?php
// Process client request VIA URL
header("Content-Type:application/json");
include("functions.php");
if(!empty($_GET['name']))
{
// if we have the name of the 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 as $book=>$price)
{
if($book=$find)
{
return $price;
break;
}
}
}
?>
的.htaccess
# Turn on the rewrite engine
Options +FollowSymlinks
RewriteEngine on
# Request routing
RewriteRule ^([a-zA-Z_-]*)$ index.php?name=$1 [nc, qsa]
虽然我在没有.htaccess文件的情况下尝试教程,但它运行良好。 但是当我把这个文件放到我的目录中时,它总是崩溃&#34; SERVER ERROR&#34;,出现500错误。
请你解释一下吗?