用curl_setopt发布一个数组 [英] Posting an array with curl_setopt

查看:67
本文介绍了用curl_setopt发布一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

附加的代码返回"Notice:Array to string conversion in ...".只是将我的数组作为包含"Array"字的字符串处理到远程服务器.其余的变量都很好.

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.

如何在没有问题的情况下传递数组$anarray?

How can I pass my array $anarray without this problem?

<?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);

?>

推荐答案

使用 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

然后您可以使用$_POST超全局变量

You can then access it normally using the $_POST superglobal

这篇关于用curl_setopt发布一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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