(PHP) $_POST +卷曲+多阵列

发布于 2024-12-06 01:10:18 字数 1169 浏览 0 评论 0原文

我尝试通过 cURL 发送多数组,但找不到好的方法。

我的代码示例:

$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' );

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

这将起作用并在 curl_init() 站点上给出我想要的结果:

print_r( $_POST )

Array (

    [a] => testa
    [b] => testb
    [c] => Array (

            [d] => test1
            [e] => test2
    )
)

我想动态添加 c 数组例如:

$c['d'] = 'test1';
$c['e'] = 'test2';

但是,如果我尝试使用 array_push[] 添加数组,我总是会在数组中得到 and (string)Array ,而没有数据。

有人可以帮我做吗?

用于更快测试的整个代码:

$url = 'url_to_test.php';
$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' );
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$buffer = curl_exec ($ch);
curl_close ($ch);

echo $buffer;

test.php

print_r($_POST);

感谢您的帮助!

干杯

I try to send a multi array via cURL but I can't find a nice way to do it.

My code example:

$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' );

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

This will work and give the result I want on the curl_init() site:

print_r( $_POST ) :

Array (

    [a] => testa
    [b] => testb
    [c] => Array (

            [d] => test1
            [e] => test2
    )
)

I'd like to add the c array dynamically like:

$c['d'] = 'test1';
$c['e'] = 'test2';

But if I try to add an array with array_push or [] I always get and (string)Array in the Array without data.

Can anyone help me to do it?

The whole code for faster testing:

$url = 'url_to_test.php';
$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' );
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$buffer = curl_exec ($ch);
curl_close ($ch);

echo $buffer;

The test.php

print_r($_POST);

Thanks for any help!

cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑脸一如从前 2024-12-13 01:10:18

您只需

$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' )

添加新的字符串键“c[d]”和“c[e]”。

如果您想要一个嵌套数组,请使用:

$data = array( 'a' => 'testa', 'b' => 'testb', 'c' => 
    array( 'd' => 'test1', 'e' => 'test2' )
)

-- 编辑 --

您正在尝试设置 POST 数据,它本质上是一组键值对。您无法提供嵌套数组。但是,您可以序列化嵌套数组并在另一端对其进行解码。例如:

$post_data = array('data' => serialize($data));

在接收端:

$data = unserialize($_POST['data']);

In

$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' )

You've simply added new string keys "c[d]" and "c[e]".

If you want a nested array, use:

$data = array( 'a' => 'testa', 'b' => 'testb', 'c' => 
    array( 'd' => 'test1', 'e' => 'test2' )
)

-- EDIT --

You're trying to set POST data, which is essentially a set of key value pairs. You can't provide a nested array. You could, however, serialize the nested array and decode it at the other end. Eg:

$post_data = array('data' => serialize($data));

And at the receiving end:

$data = unserialize($_POST['data']);
诠释孤独 2024-12-13 01:10:18

这是您的解决方案

$urltopost = "http://example.com/webservice/service.php";
$datatopost = array (0 =>array('a'=>'b','c'=>'d'),1 =>array('a'=>'b','c'=>'d'),2 =>array('a'=>'b','c'=>'d'),3 =>array('a'=>'b','c'=>'d'));
$post_data = array('data' => serialize($datatopost));

$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch);

echo "<pre>";
print_r(unserialize($returndata));

service.php 代码

$temp = unserialize($_POST['data']);
echo serialize($temp);

here is your solution

$urltopost = "http://example.com/webservice/service.php";
$datatopost = array (0 =>array('a'=>'b','c'=>'d'),1 =>array('a'=>'b','c'=>'d'),2 =>array('a'=>'b','c'=>'d'),3 =>array('a'=>'b','c'=>'d'));
$post_data = array('data' => serialize($datatopost));

$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch);

echo "<pre>";
print_r(unserialize($returndata));

service.php code

$temp = unserialize($_POST['data']);
echo serialize($temp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文