这是我想从用户和wnat中获取详细信息的代码使用1.rest 2.post方法3.xml 4.php代码,不想使用cURL。在find.php文件中我想要json到xml并且我可以在没有卷曲的情况下完成它吗?
这是function.php
<?php
function get_price($find){
$books=array(
"java"=>300,
"c"=>250,
"php"=>350);
foreach($books as $books=>$price)
{
if($books==$find)
{
return $price;
break;
}
}
}
?>
&#13;
此代码适用于index.php
<?php
header("content-Type:application/json");
include("function.php");
if(!empty($_GET['name'])){
$name = $_GET['name'];
$price = get_price($name);
if(empty($price)){
deliver_response(200,"book not found",NULL);
}
else{
deliver_response(200,"book found",$price);
}
}
else{
deliver_response(400,"invalid",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;
}
?>
&#13;
错误在此代码中我想使用xml
<!DOCTYPE html>
<html lang="en">
<head>
<title>Rest</title>
</head>
<body>
<h2>ENTER BOOK :</h2>
<form action = "" method = "POST">
<input type = "text" name = "name" /> <br />
<input type = "submit" name = "submit" />
</form>
<?php
header("Content-type: text/xml");
if(isset($_POST['submit']))
$name = $_POST['name'];
$url="http://localhost/rest/find.php?name=$name";
$client=curl_int($url);
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);
$response=curl_exec($client);
$result=simplexml_load_file($response);
echo $result->data;
print_r($result);
?>
</body>
</html>
&#13;