想用php

时间:2016-07-14 10:47:27

标签: php html xml rest curl

这是我想从用户和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;
&#13;
&#13;

此代码适用于index.php

&#13;
&#13;
<?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;
&#13;
&#13;

错误在此代码中我想使用xml

&#13;
&#13;
<!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;
&#13;
&#13;

0 个答案:

没有答案