用php和json制作的书籍的休息服务

时间:2017-04-26 09:07:43

标签: php json rest

您好我正在关注对我的项目非常有帮助的教程,但我无法弄清楚错误是什么: 以下是restClient文件夹中index.php的代码: first the html code :

ENTER BOOK:

           
            `...... 和PHP代码:

if(isset($_POST['submit'])){
    $name = $_POST['name'];

    //Resourse Address        
    $url = "http://localhost/jelenavulesevic3i4domaci/rest/$name";

    //Send request to Resourse        
    $client = curl_init($url);

    curl_setopt($client,CURLOPT_RETURNTRANSFER,1);

    //get response from resource
    $response = curl_exec($client);

    //echo $response;

    //decode        
    $result = json_decode($response);

    echo $result->data; //THIS WONT WORK

}

我在rest文件夹中有一个不同的index.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 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如下:

function get_price($find){
    $books = array(
        "java" => 299,
        "c" => 343,
        "php" => 223
    );

    foreach($books as $book=>$price){
        if($book == $find)
        {
            return $price;
            break;
        }
    }
}

我的错误是在index.php的一行中为客户端说的:  echo $ result-> data;

有人能帮助我吗?

0 个答案:

没有答案