如何在 PHP 中启动 GET/POST/PUT/DELETE 请求并判断请求类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2153650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:15:29  来源:igfitidea点击:

How to start a GET/POST/PUT/DELETE request and judge request type in PHP?

phprequest

提问by user198729

I never see how is PUT/DELETErequest sent.

我从来没有看到PUT/DELETE请求是如何发送的。

How to do it in PHP?

如何在 PHP 中做到这一点?

I know how to send a GET/POST request with curl:

我知道如何使用 curl 发送 GET/POST 请求:

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookieFile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,   CURLOPT_SSL_VERIFYPEER,   FALSE);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);

But how to do PUT/DELETErequest?

但是怎么做PUT/DELETE请求?

回答by Gordon

For DELETEuse curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
For PUTuse curl_setopt($ch, CURLOPT_PUT, true);

对于DELETE使用curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
对于PUT使用curl_setopt($ch, CURLOPT_PUT, true);

An alternative that doesn't rely on cURL being installed would be to use file_get_contentswith a custom HTTP stream context.

不依赖于安装 cURL 的替代方法是file_get_contents自定义 HTTP 流上下文一起使用

$result = file_get_contents(
    'http://example.com/submit.php', 
    false, 
    stream_context_create(array(
        'http' => array(
            'method' => 'DELETE' 
        )
    ))
);

Check out these two articles on doing REST with PHP

查看这两篇关于使用 PHP 进行 REST 的文章

回答by Pascal MARTIN

Generally speaking, if you want to send some "non-GET" request, you'll often work with curl.

一般来说,如果您想发送一些“非 GET”请求,您通常会使用curl


And you'll use the curl_setoptfunction to configure the request you're sending ; amongst the large amount of possible options, to change the request method, you'll be interested by at least those options (quoting):


您将使用该curl_setopt函数来配置您发送的请求;在大量可能的选项中,要更改请求方法,您至少会对这些选项感兴趣(引用)

  • CURLOPT_CUSTOMREQUEST: A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request. This is useful for doing "DELETE" or other, more obscure HTTP requests.
  • CURLOPT_HTTPGET: TRUEto reset the HTTP request method to GET.
  • CURLOPT_POST: TRUEto do a regular HTTP POST.
  • CURLOPT_PUT: TRUEto HTTP PUTa file. The file to PUTmust be set with CURLOPT_INFILEand CURLOPT_INFILESIZE.
  • CURLOPT_CUSTOMREQUEST:在执行 HTTP 请求时使用的自定义请求方法而不是“ GET”或“ HEAD”。这对于执行 " DELETE" 或其他更模糊的 HTTP 请求很有用。
  • CURLOPT_HTTPGET:TRUE将 HTTP 请求方法重置为GET.
  • CURLOPT_POST:TRUE做一个常规的 HTTP POST
  • CURLOPT_PUT: TRUEHTTPPUT文件。PUT要设置的文件必须使用CURLOPT_INFILECURLOPT_INFILESIZE


Of course, curl_setoptis not the only function you'll use ; see the documentation page of curl_execfor an example of how to send a request with curl.


当然,curl_setopt这不是您要使用的唯一功能;curl_exec有关如何使用 curl 发送请求的示例,请参阅文档页面。

(Yes, that example is pretty simple, and sends a GETrequest -- but you should be able to build from there ;-) )

(是的,这个例子非常简单,并发送了一个GET请求——但你应该能够从那里构建;-))