发布使用PHP和卷曲多维数组 [英] Posting multidimensional array with PHP and CURL
问题描述
我在通过卷曲麻烦发布表单数据位于不同的主机上接收PHP脚本。
I'm having trouble posting form data via CURL to a receiving PHP script located on a different host.
我得到一个阵列字符串转换
错误
这是的print_r
数组我张贴的:
Array
(
[name] => Array
(
[0] => Jason
[1] => Mary
[2] => Lucy
)
[id] => 12
[status] => local
[file] => @/test.txt
)
这是发生在错误的行:
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
第三个参数必须是一个数组,因为我需要在内容类型
头设置为的multipart /表单数据
,因为我通过这个同一阵列发送一个文件,所以我可以在阵列不能转换为查询字符串或使用 http_build_query()
。
The third argument must be an array because I need the Content-Type
header to be set to multipart/form-data
as I am sending a file via this same array, therefore I cannot convert the array to a query string or use http_build_query()
.
此外,我没有进入到接收主机上的code,所以我不能序列化和反序列化数组。
Also I do not have access to the code on the receiving host so I cannot serialize and unserialize the array.
我假设的 name的值的键是一个数组是此错误的原因,我也假设 CURLOPT_POSTFIELDS
不支持多维数组。有没有解决这个任何其他方式或者我注定?
I'm assuming that the value of the name key being an array is the cause for this error, I'm also assuming that CURLOPT_POSTFIELDS
doesn't support multidimensional arrays. Is there any other way around this or am I doomed?
在此先感谢!
推荐答案
您不得不手动构建POST字符串,而不是传递整个数组,然后你可以重写卷曲的自动选择内容的标头:
You'd have to build the POST string manually, rather than passing the entire array in. You can then override curl's auto-chose content header with:
curl_setopt($c, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
序列化/ JSON-ifying会更容易些,但正如你所说,你有在接收端无法控制,所以你有一些额外的工作要做。
Serializing/json-ifying would be easier, but as you say, you have no control over the receiving end, so you've got a bit of extra work to do.
这篇关于发布使用PHP和卷曲多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!