php 使用 curl_setopt 发布数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2158138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Posting an array with curl_setopt
提问by Essam
The attached code is returning "Notice: Array to string conversion in...". Simply my array is being handled to the remote server as a string containing "Array" word. the rest of the variables are fine.
附加的代码返回“注意:数组到字符串的转换...”。只是我的数组作为包含“数组”字的字符串被处理到远程服务器。其余的变量都很好。
How can I pass my array $anarraywithout this problem?
如何在$anarray没有这个问题的情况下传递我的数组?
<?php
$data = array(
'anarray' => $anarray,
'var1' => $var1,
'var2' => $var2
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "MY_URL");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
回答by John Conde
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// The values of variables will be shown but since we don't have them this is what we get
You can then access it normally using the $_POSTsuperglobal
然后您可以使用$_POSTsuperglobal正常访问它
回答by Eric Butera
The best way to accomplish what you're after is to use http_build_query().
完成您所追求的事情的最佳方法是使用http_build_query().
回答by Tim Dearborn
From http://www.php.net/manual/en/function.curl-setopt.phpdescription of CURLOPT_POSTFIELDS
来自http://www.php.net/manual/en/function.curl-setopt.php对 CURLOPT_POSTFIELDS 的描述
The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.
要在 HTTP“POST”操作中发布的完整数据。要发布文件,请在文件名前加上 @ 并使用完整路径。文件类型可以通过在文件名后面加上“;type=mimetype”格式的类型来明确指定。此参数可以作为 urlencoded 字符串(如 'para1=val1¶2=val2&...')或作为以字段名称作为键和字段数据作为值的数组传递。如果 value 是一个数组,则 Content-Type 标头将设置为 multipart/form-data。从 PHP 5.2.0 开始,如果文件以 @ 前缀传递给此选项,则 value 必须是一个数组。
回答by James Cache
Due to the nature of the HTTP protocol, and the way curl_setopt function works, $anarray cannot be passed directly as an array.
由于 HTTP 协议的性质以及 curl_setopt 函数的工作方式,$anarray 不能直接作为数组传递。
The following statement:
以下声明:
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
takes an array of POST parameters and for each of them there must be a string name and a STRINGvalue. You are passing an array value instead, so the PHP processor is forced to convert it to a string using some lame built-in algorithm, which incurs issuance of the before-mentioned notice ("Array to string conversion in...").
接受一个 POST 参数数组,每个参数都必须有一个字符串名称和一个STRING值。您正在传递一个数组值,因此 PHP 处理器被迫使用一些蹩脚的内置算法将其转换为字符串,这会导致发出上述通知(“数组到字符串转换...”)。
So, in order to properly pass that array ($anarray) to the other side, you have to take care of its encoding (into a string) yourself, as well as the other side has to take care of its decoding (from a string).
因此,为了正确地将该数组 ($anarray) 传递给另一方,您必须自己处理它的编码(转换为字符串),另一方必须处理其解码(从字符串)。
My approach in such situations is JSON. It is suitable enough in almost all cases. All you have to do is apply the following technique:
我在这种情况下的方法是 JSON。它几乎适用于所有情况。您所要做的就是应用以下技术:
$data=array(
'anarray'=>json_encode($anarray),
'var1'=>$var1,
'var2'=>$var2
);
And then, on the other side of the connection you would retrieve the original array the following way:
然后,在连接的另一端,您将通过以下方式检索原始数组:
$anarray=json_decode($_POST['anarray'],true); // true indicates for associative array rather than an object
回答by GZipp
If $anarray is an array, as I suspect it is, it shouldn't be. Turn it into a string, by concatenating or whatever appropriate method.
如果 $anarray 是一个数组,正如我怀疑的那样,它不应该是。通过连接或任何适当的方法将其转换为字符串。
Edit: See Eric Butera's answer.
编辑:请参阅 Eric Butera 的回答。

