简体   繁体   English

如何使用 curl 发送 PHP 输入 stream 数据?

[英]How to send PHP input stream data using curl?

POST邮政

On my local machine, I want to use cURL as an HTTP client like so:在我的本地机器上,我想使用cURL作为 HTTP 客户端,如下所示:

curl -X POST -F 'email=derp@example.com' -F 'password=blink182' http://example.com

The above curl statement uses the HTTP POST method which can be retrieved in PHP like so:上面的 curl 语句使用 HTTP POST 方法,可以在 PHP 中检索,如下所示:

echo $_POST['email'];       // derp@example.com
echo $_POST['password'];    // blink182

php://input php://input

However, what I really wanted is the data from the PHP input stream php:://input and not from the POST method $_POST .但是,我真正想要的是来自PHP 输入 stream php:://input的数据,而不是来自POST 方法$_POST的数据。

The PHP input stream can be retrieved in PHP like so: PHP 输入 stream 可以在 PHP 中检索,如下所示:

$input = json_decode( file_get_contents( 'php://input' ) );
echo $input->email;     // derp@example.com
echo $input->password;  // blink182

Which brings me to my question, how to send PHP input stream data using curl?这让我想到了我的问题,如何使用 curl 发送 PHP 输入 stream 数据?

From the PHP website :来自PHP 网站

php://input is a read-only stream that allows you to read raw data from the request body. php://input是只读的 stream,允许您从请求正文中读取原始数据。 php://input is not available with enctype="multipart/form-data" . php://input不适用于enctype="multipart/form-data"

So if you specify the Content-Type as application/json using -H "Content-Type: application/json" you ought to get it in the input stream因此,如果您使用-H "Content-Type: application/json"Content-Type指定为application/json ,您应该在输入 stream 中获取它

complete example:完整的例子:

curl -X POST https://reqbin.com/echo/post/json
   -H 'Content-Type: application/json'
   -d '{"email":"derp@example.com","password":"blink182"}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM