【问题标题】:How do you get receive data with a PHP rest api?如何使用 PHP rest api 接收数据?
【发布时间】:2016-08-25 18:47:22
【问题描述】:

我正在输入这些数据 {id="1", title="foo", imagedescription="bar"}

这是我的 PHP 端点

<?php

// This will update a photo that has been edited
if (isset($_GET['id'], $_GET['title'], $_GET['imagedescription']))
{
$id=   $_GET['id'];
$title=  trim($_GET['title']);
$imagedescription=trim($_GET['imagedescription']);

require 'db_connect.php';

$update_query = "Update images Set title = {$title}, imagedescription = {$imagedescription} Where id={$id}";

    if($update = $db->query($update_query))
    {
        $response["success"] = 1;
        $response["message"] = "Photo successfully updated.";
        // echoing JSON response
        echo json_encode($response);
    } 
    else 
    {
        $response["failed"] = 0;
        $response["message"] = "Oops! An error occurred.";
        $response["sql"] = $update_query;

        // echoing JSON response
        echo json_encode($response);
    }

}
else
{
// required field is missing
$response["failed"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}

?>

我刚刚在 Postman 中收到 {"failed":0,"message":"Required field(s) is missing"} 和 200 成功响应。

如何使用 PHP 从请求中获取数据?

这是 Angular 服务

(function () {
angular.module('app')
    .service('PhotosManager', ['$http', function($http) {

            this.getPhotos = function () {

                return $http.get("get_all_photos.php");
            };

            this.updatePhoto = function (id, data) {
                return $http.put("update_photo.php"+ id, data);

            };

    }]);

})();

我可以看到成功的响应 请求方法:PUT 状态码:200 OK

【问题讨论】:

  • 您正在输入这些数据{id="1", title="foo", imagedescription="bar"} 知道了。但是来自 ajax 或来自 rest 客户端,它是 GET 还是 POST?
  • $http.put("update_photo.php"+ id, data);
  • 包含 JS 部分,这样会更有帮助

标签: php rest put


【解决方案1】:

这行得通。先读取已经放入的数据,然后json_decode,最后就可以从数组中访问了。

<?php


$putfp = fopen('php://input', 'r');
$putdata = '';
while($data = fread($putfp, 1024))
$putdata .= $data;
fclose($putfp);


$data = json_decode($putdata, true);

// This will update a photo that has been edited
if (isset($data['id'], $data['title'], $data['imagedescription']))
{
$id=   $data['id'];
$title=  trim($data['title']);
$imagedescription=trim($data['imagedescription']);

require 'db_connect.php';

$update_query = "Update images Set title = '{$title}', imagedescription = '{$imagedescription}' Where id={$id}";

    if($update = $db->query($update_query))
    {
        $response["success"] = 1;
        $response["message"] = "Photo successfully updated.";
        // echoing JSON response
        echo json_encode($response);
    } 
    else 
    {
        $response["failed"] = 0;
        $response["message"] = "Oops! An error occurred.";
        $response["sql"] = $update_query;

        // echoing JSON response
        echo json_encode($response);
    }

}
else
{
// required field is missing
$response["failed"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2020-02-06
    • 2017-03-16
    • 2021-09-25
    • 2013-07-24
    相关资源
    最近更新 更多