使用curl_setopt发布数组

Ess*_*sam 18 php curl libcurl

附加的代码返回"注意:数组到字符串转换...".只是我的数组被作为包含"Array"字符的字符串处理到远程服务器.其余的变量都很好.

如何在$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);

?>
Run Code Online (Sandbox Code Playgroud)

Joh*_*nde 30

使用 http_build_query()

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
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用$_POST超全局正常访问它

  • `serialize`为此操作生成完全错误格式的数据 (3认同)
  • http_build_query很好,那些downvotes和评论必须是答案的旧版本 (2认同)

Eri*_*era 15

实现目标的最佳方法是使用http_build_query().

  • 这样cURL将生成一个`application/x-www-form-urlencoded` HTTP请求,除非你想上传一个需要`multipart/form-data`请求的文件,否则这样做很好. (2认同)