Php 无法通过CURL传递数组?
如果我将其发布到自身并执行Php 无法通过CURL传递数组?,php,arrays,curl,arraylist,php-curl,Php,Arrays,Curl,Arraylist,Php Curl,如果我将其发布到自身并执行var_dump($_post[“items”])我刚刚得到字符串(5)“Array”作为输出 我还尝试了一个foreach循环,它不输出任何数据 我是不是很愚蠢,而且出了什么大问题?你可以试试这个 $items[] = array("sku"=>"data","name"=>"data","amount"=>0,"qty"=>"0","id"=>"data","price"=>0,"url"=>"data"); $post
var_dump($_post[“items”])代码>我刚刚得到字符串(5)“Array”
作为输出
我还尝试了一个foreach循环,它不输出任何数据
我是不是很愚蠢,而且出了什么大问题?你可以试试这个
$items[] = array("sku"=>"data","name"=>"data","amount"=>0,"qty"=>"0","id"=>"data","price"=>0,"url"=>"data");
$post = array(
'data' => 'data',
'items' => $items);
$ch = curl_init('urltopost');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 3); //timeout in seconds
header('Content-Type: text/html');
echo curl_exec($ch);
// Check if any error occurred
if(curl_errno($ch))
{
header('Location: error');
die();
}
post数组格式为array('key'=>string,…)
,不允许使用数组。如果需要post数组,则密钥格式为'key[index1][index2]…'
。在服务器中,您将收到数组形式的$\u POST['key']
。var\u dump($\u POST)
我想您会看到问题,它提供了我想要的所有数据,您能向我指出问题吗?$items[]
。。创建新维度,使其最终成为$items[0]
。。您可以只使用$items=array()
@smith I将其更改为$items=array();它没有任何区别sadlySettingcurl_setopt($ch,CURLOPT_POSTFIELDS,$items)代码>也应该起作用。从docs向CURLOPT_POSTFIELDS传递数组将数据编码为多部分/表单数据,而传递URL编码字符串将数据编码为application/x-www-form-urlencoded。
。在您的情况下,问题似乎是您正在用另一个数组包装$items。
$post = [];
foreach($items as $index=>$item){
$new_key = "item[$index]";
foreach($item as $k=>$v){
$post[$new_key.'['. $k .']'] = $v;
}
}
$post['data'] = 'data';