cURL自定义二进制数据的POST(不是表单内容) [英] cURL POST of custom binary data (not form contents)

查看:1033
本文介绍了cURL自定义二进制数据的POST(不是表单内容)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段成功发送POST请求,但不发送$ sendStream中的数据(流是有效的,包含数据 - 已验证)的问题:

The problem the following code piece successfully sends POST request but doesn't send the data in $sendStream (the stream is valid and contains data - this has been verified):

curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-type: application/x-rethync-request'));
curl_setopt($request, CURLOPT_HEADER, true);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_INFILE, $sendStream);
curl_setopt($request, CURLOPT_INFILESIZE, stream_length($sendStream));
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "POST");

$response = curl_exec($request);

我已经读过大概所有cURL POST相关的帖子在这里,但没有运气。为什么数据未发布?

I've read probably all cURL POST-related posts here on SO, but no luck. Why is the data not posted?

推荐答案

缺少的是

    curl_setopt($request, CURLOPT_UPLOAD, 1);

PHP文档简要提到了这个选项,没有什么建议应该设置该选项。仍然至少一些(如果不是全部)版本的cURL不发布数据,尽管它通过CURLOPT_INFILE指定,除非CURLOPT_UPLOAD也被设置。

The PHP documentation mentions this option briefly and nothing suggests that the option should be set. Still at least some (if not all) versions of cURL don't post data despite it's specified via CURLOPT_INFILE, unless CURLOPT_UPLOAD is also set.

注意:当您使用此方法发送数据时,响应头将包含 HTTP / 1.1 100 Continue 首先是 HTTP / 1.1 200 OK ,依此类推。因此,当您解析响应标头时,请注意第一个100继续响应(您需要删除它)。

NOTE: when you use this method to send the data, the response header will contain HTTP/1.1 100 Continue first, followed by HTTP/1.1 200 OK and so on. So when you parse response headers, beware of the first 100 Continue response (you'll need to strip it).

这篇关于cURL自定义二进制数据的POST(不是表单内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆