Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP REST放置/删除选项_Php_Rest - Fatal编程技术网

PHP REST放置/删除选项

PHP REST放置/删除选项,php,rest,Php,Rest,试图理解在PHP中创建应用程序的REST方法 我在理解如何从php脚本发送put/delete时遇到问题 在internet上,我只能找到如何确定发送了哪个php方法 if($_SERVER['REQUEST_METHOD'] == 'DELETE') 但是如何发送这个DELETE方法呢 当我想从DB中删除一些记录时,我通常会做什么?我有一个普通的html表单,方法设置为post/get并记录DB id,然后我按submit按钮发送post/get表单 如何创建此提交发送删除/放置方法 如果您

试图理解在
PHP
中创建应用程序的
REST
方法

我在理解如何从php脚本发送
put
/
delete
时遇到问题

在internet上,我只能找到如何确定发送了哪个php方法

if($_SERVER['REQUEST_METHOD'] == 'DELETE')
但是如何发送这个
DELETE
方法呢

当我想从
DB
中删除一些记录时,我通常会做什么?我有一个普通的html表单,方法设置为
post
/
get
并记录DB id,然后我按submit按钮发送
post
/
get
表单


如何创建此提交发送
删除
/
放置
方法

如果您使用的是Chrome,您可以使用来测试REST服务。它允许发送任何类型的命令-删除、放置,还有选项、补丁等


在Firefox上,您可以使用其他方法。

HTML表单只支持GET和POST,因此在普通web应用程序中,您需要使用隐藏字段来指定请求方法,这是大多数框架所做的

<form method="post" action="...">
  ...
  <input type="hidden" name="REQUEST_METHOD" value="PUT" />
<form>

...

通常的方法是使用
cURL

$ch = curl_init('YOUR_URL');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); // curl_setopt($ch, CURLOPT_PUT, true); - for PUT
curl_setopt($ch, CURLOPT_POSTFIELDS, 'some_data');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);  // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // RETURN THE CONTENTS OF THE CALL
$result = curl_exec($ch);

使用GET或POST以外的http方法从HTML页面发送请求有两种常见方法

#1:使用html表单发送POST请求,但包含一个隐藏的表单字段,告诉服务器将请求视为使用不同的方法。这就是方法

注意:您可以将上述逻辑放入每个页面(或从每个页面调用)。另一种方法是构建代理脚本(例如,
“rest-form-proxy.php”
)。然后,站点中的所有表单都将提交给代理,包括请求方法和目标url。代理将提取提供的信息,并使用正确的请求http方法将请求转发到所需的url

代理方法是在每个脚本中嵌入逻辑的一个很好的替代方法。如果您确实构建了代理,请确保检查请求的URL,并且不允许任何不指向您自己站点的URL。未能执行此检查将允许其他人使用您的代理在其他网站上发起恶意攻击;而且它还可能危及您网站的安全和/或隐私

--

#2:在HTML页面中使用Javascript启动一个新的脚本。这是一种更复杂的方法,需要一点javascript,但在某些情况下可以更灵活。它允许您向服务器发送请求,而无需重新加载页面。它还允许您以多种不同的格式发送数据(您不仅限于从html表单发送数据)。例如:

<button onclick="doSave()">Save</button>

<script>
    var myObject = {
       // ... some object properties that 
       // that you'll eventually want to save ...
    };

    function doSave() {
        var xhr = createxmlhttprequest();

        // initialize the request by specifying the method 
        // (ie: "get", "put", "post", "delete", etc.), and the
        // url (in this case, "my_resource.php").  The last param
        // should always be `true`.

        xhr.open("put", "my_resource.php", true);
        xhr.setRequestHeader('Content-Type', 'application/json');

        xhr.onreadystatechange = function() {
           if (xhr.readystate != 4) { return; }
           var serverresponse = xhr.responsetext;

           // ... this code runs when the response comes back
           // from the server.  you'll have to check for success
           // and handle the response document (if any).
        };

        // this initiates the request, sending the contents
        // of `myObject` as a JSON string.  

        xhr.send(JSON.stringify(myObject));

        // The request runs in the background
        // The `onreadystatechange` function above
        // detects and handles the completed response.
    }
</script>
保存
变量myObject={
//…某些对象属性
//你最终会想要拯救。。。
};
函数doSave(){
var xhr=createxmlhttprequest();
//通过指定方法初始化请求
//(即:“获取”、“放置”、“发布”、“删除”等),以及
//url(在本例中为“my_resource.php”)。最后一个参数
//应该始终为“真”。
open(“put”,“my_resource.php”,true);
setRequestHeader('Content-Type','application/json');
xhr.onreadystatechange=函数(){
如果(xhr.readystate!=4){return;}
var serverresponse=xhr.responsetext;
//…此代码在响应返回时运行
//从服务器。您必须检查是否成功
//并处理响应文件(如有)。
};
//这将启动请求,并发送内容
//将'myObject'作为JSON字符串。
send(JSON.stringify(myObject));
//请求在后台运行
//上面的“onreadystatechange”函数
//检测并处理已完成的响应。
}
XMLHttpRequest比我在上面的基本示例中展示的要多得多。如果你选择这条路线,请仔细研究。除其他事项外,请确保正确处理各种错误条件。跨浏览器兼容性也存在许多问题,其中许多问题可以通过使用中介解决,如


最后,我应该指出,上述两种方法并不是相互排斥的。对某些请求使用表单,对其他请求使用XMLHttpRequest是完全可能的,只要您构建的服务器能够处理任何一种请求(如上面的#1所示)。

使用
curl
curl\u setopt($ch,CURLOPT\u CUSTOMREQUEST,'DELETE')
PUT将是
curl\u setopt($ch,CURLOPT\u PUT,true)看看这个图坦卡蒙,它似乎涵盖了一些旧的东西,但会给你一个想法来实现你自己的。你不是试图发送一个请求“从php脚本”。您正试图从由php脚本生成的HTML页面发送请求。PHP脚本在服务器上运行,而生成的HTML页面在浏览器中运行。这个错误的陈述就是为什么你得到的答案集中在基于php的工具上,比如
curl
——但是这些工具在浏览器中不可用。在浏览器中,您需要使用javascript发送表单,或者遵循下面的[@xdazz建议](#12085776)是的,了解curl在服务器上的工作原理,表单是从html文档发送的。但我想我不明白一些基本的事情。不知道为什么要用“我想这和我删除一些记录时总是做的一样。我正在添加名为“action”的隐藏字段,并将此操作设置为“delete”或“update”,并基于此变量在DB上执行一些操作。我想在发送REQUEST_方法隐藏var之后,我们必须在php服务器上做一些事情,但不确定what@abiku抱歉,我的回复有点晚,但直到今天我才看到你的评论。我在下面添加了一个答案,希望能解决您的一些问题。初始问题
如何从php脚本发送put/delete?/* my_resource.php */

$method = strtolower($_SERVER['REQUEST_METHOD']);
if( $method === 'post' && isset($_REQUEST['REQUEST_METHOD'])) {
    $tmp = strtolower((string)$_REQUEST['REQUEST_METHOD']);
    if( in_array( $tmp, array( 'put', 'delete', 'head', 'options' ))) {
        $method = $tmp;
    }
    unset($tmp);
}

// now, just run the logic that's appropriate for the requested method
switch( $method ) {
    case "get":
        // logic for GET here
        break;

    case "put":
        // logic for PUT here
        break;        

    case "post":
        // logic for POST here
        break;

    case "delete":
        // logic for DELETE here
        break;

    case "head":
        // logic for DELETE here
        break;

    case "options":
        // logic for DELETE here
        break;

    default:
        header('HTTP/1.0 501 Not Implemented');
        die();
}
<button onclick="doSave()">Save</button>

<script>
    var myObject = {
       // ... some object properties that 
       // that you'll eventually want to save ...
    };

    function doSave() {
        var xhr = createxmlhttprequest();

        // initialize the request by specifying the method 
        // (ie: "get", "put", "post", "delete", etc.), and the
        // url (in this case, "my_resource.php").  The last param
        // should always be `true`.

        xhr.open("put", "my_resource.php", true);
        xhr.setRequestHeader('Content-Type', 'application/json');

        xhr.onreadystatechange = function() {
           if (xhr.readystate != 4) { return; }
           var serverresponse = xhr.responsetext;

           // ... this code runs when the response comes back
           // from the server.  you'll have to check for success
           // and handle the response document (if any).
        };

        // this initiates the request, sending the contents
        // of `myObject` as a JSON string.  

        xhr.send(JSON.stringify(myObject));

        // The request runs in the background
        // The `onreadystatechange` function above
        // detects and handles the completed response.
    }
</script>