且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP CURL:手动设置content-length标头

更新时间:2022-06-09 23:29:17

要获得一个帖子正文的长度,请尝试格式化一个GET样式字符串的字段(aka param1 = value1& param2 = value2 )然后将该字符串设置为 CURL_POSTFIELDS with curl_setopt 。不必提供数组。您可以使用 strlen()来获取用于内容长度标头的值。

To get the length of a post body, try formatting the fields int a GET style string (aka param1=value1&param2=value2) then setting that string as the CURL_POSTFIELDS with curl_setopt. An array does not have to be supplied. You can simply use strlen() to get the value to use for the content-length header.

除了其他字段之外,还要发布一个文件(或多个文件),如上面的例子所示,您必须提供文件的值为 @ / path / to / file $

If you are posting a file (or files) in addition to other fields, as you appear to be in the example above, you have to supply the value for the file as @/path/to/file, then get the filesize in bytes and add that to the total content-length.

因此对于上面的例子,假设文件 test.txt 在您的服务器的 / test dir 中,发布值字符串将是 file_name = @ / test / text.txt& submit = UPLOAD 。你必须 url_encode 这个字符串,在你分配它作为curl post值之前。要获取内容长度,您将获得该字符串的长度(后置网址编码),并将将其添加到 / test / test的filesize 。 txt 。

So for the above example, assuming the file test.txt is in the /test dir of your server, the post value string would be file_name=@/test/text.txt&submit=UPLOAD. You MUST url_encode this string as well, before you assign it as the curl post value. To get the content length you get the length of that string (post url-encoding) and add it to the filesize of /test/test.txt.