php中的自定义API和cunsuming?

php中的自定义API和cunsuming?,php,rest,curl,Php,Rest,Curl,我想在php中使用Curl编写Web服务(REST)和消费 $books = array( "java"=>"222", "php"=>"333", "c"=>"111", "AngularJS"=>"111" ); 要创建API,请执行以下操作: <?php $books = array( "java"=>"222", "php"=>"333",

我想在php中使用Curl编写Web服务(REST)和消费

$books = array(
        "java"=>"222",
        "php"=>"333",
        "c"=>"111",
        "AngularJS"=>"111"
        );

要创建API,请执行以下操作:

<?php
$books = array(
    "java"=>"222",
    "php"=>"333",
    "c"=>"111",
    "AngularJS"=>"111"
);

return json_encode($books);

首先定义API和客户端URL的URL端点

ex:API: http://www.customapi.com/java
   Client URI: http://www.clientcustomapi.com/
API代码片段: index.php

header("Content-Type: application/json;charset=utf-8");
include('functions.php');
//process client request
if(!empty($_GET['name'])){
    $name = $_GET['name'];
    $price = get_price($name);
    if(empty($price)){
        //book not found
        deliveryResponse(200,"Book not found",NULL);
    }else{
        //response book price
        deliveryResponse(200,"Book found",$price);
    }
}else{
    //invalid request
    deliveryResponse(400,"invalid Request",NULL);
}
function get_price($find){
    $books = array(
        "java"=>"222",
        "php"=>"333",
        "c"=>"111"
        );
    foreach ($books as $book => $price) {
        # code...
        if($book==$find){
            return $price;
            break;
        }
    }

}
function deliveryResponse($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

header("Content-Type: application/json;charset=utf-8");
include('functions.php');
//process client request
if(!empty($_GET['name'])){
    $name = $_GET['name'];
    $price = get_price($name);
    if(empty($price)){
        //book not found
        deliveryResponse(200,"Book not found",NULL);
    }else{
        //response book price
        deliveryResponse(200,"Book found",$price);
    }
}else{
    //invalid request
    deliveryResponse(400,"invalid Request",NULL);
}
function get_price($find){
    $books = array(
        "java"=>"222",
        "php"=>"333",
        "c"=>"111"
        );
    foreach ($books as $book => $price) {
        # code...
        if($book==$find){
            return $price;
            break;
        }
    }

}
function deliveryResponse($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;
}
客户端代码片段:

<!DOCTYPE html>
<html>
<head>
    <title>Book Price</title>
</head>
<body>
<form method="post" action="" name="bookprice">
    <label>Book Name:</label><input type="text" name="book" id="book">
    <input type="submit" value="submit" name="submit">
</form>
</body>
</html>
<?php 
if (isset($_POST['submit'])) {
    //simple Request
    $name = $_POST['book'];
    //resource address
    $url ="http://www.customapi.com/$name";

    //send request to resource
    $client = curl_init($url);
    curl_setopt($client,CURLOPT_RETURNTRANSFER, 1);
    //get response from resource
    $response = curl_exec($client);
    $result = json_decode($response);
    if($result->data !=null){
        echo $result->data;
    }else{
        echo"No record found";
    }


}
?>

书价
书名:

如果要在php中构建api,请检查

这是一个很好的框架,有很好的文档。我建议您使用现有的解决方案,因为从头开始构建api需要大量的时间和专业知识


也是一个很好的工具,可以帮助您定义/设计rest端点

问题是什么?我想用PHP编写使用rest的Web服务?我是软件开发新手。让我们假设,我有上面的示例数据。这不是使用SO的方式。在尝试解决方案后,您需要问一个特定的问题,而不仅仅是说明您想做什么,并希望代码神奇地出现。您可以使用slim framework for Web services。将数据保存在MySQL中,并从中上载文件“api.php”。免责声明:我是作者。注意:在项目的“示例”目录中也有示例客户。谢谢!解决方案非常简单,但同样的解决方案应该在slim框架中实现,而不是在核心PHP中实现。它可能会更好。感谢您的解决方案,它可以工作,但我们如何设置url(域)?您的意思是说虚拟主机?参考:这个答案是正确的,回答了这个问题,我很好奇为什么会投反对票?