【问题标题】:How to send PHP input stream data using curl?如何使用 curl 发送 PHP 输入流数据?
【发布时间】:2021-11-18 08:24:28
【问题描述】:

发布

在我的本地机器上,我想像这样使用 cURL 作为 HTTP 客户端:

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

上面的 curl 语句使用了 HTTP POST 方法,可以像这样在 PHP 中检索:

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

php://输入

但是,我真正想要的是来自 PHP input stream php:://input 而不是来自 POST method $_POST 的数据。

可以像这样在 PHP 中检索 PHP 输入流:

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

这让我想到了我的问题,如何使用 curl 发送 PHP 输入流数据?

【问题讨论】:

    标签: php curl command-line-interface


    【解决方案1】:

    来自PHP website

    php://input 是一个只读流,允许您从请求正文中读取原始数据。 php://input 不适用于 enctype="multipart/form-data"

    因此,如果您使用-H "Content-Type: application/json"Content-Type 指定为application/json,您应该在输入流中得到它

    完整示例:

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

    【讨论】:

    • 我尝试过您的建议:curl -H "Content-Type: application/json" 'email=derp@example.com' -F 'password=blink182' http://example.com,但效果不佳。
    • 啊,您还需要发送实际的 json - curl 不会为您将表单字段转换为 json。我在答案中添加了一个示例
    猜你喜欢
    • 2012-10-17
    • 2010-11-08
    • 2015-05-12
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2018-12-21
    • 2021-04-30
    • 2020-09-26
    相关资源
    最近更新 更多