简体   繁体   中英

Unable to pass an ARRAY through CURL?

$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();
}

If I post it to itself and do var_dump($_POST["items"]); I just get string(5) "Array" as the output.

I also attempted a foreach loop which outputted no data.

Am I being stupid and something glaringly wrong is up with it?

You can try this

$post = [];
foreach($items as $index=>$item){
    $new_key = "item[$index]";
    foreach($item as $k=>$v){
        $post[$new_key.'['. $k .']'] = $v;
    }

}
$post['data'] = 'data';

The post array format is array('key'=>string, ....) , it not allow array. If you want post array, the key format is 'key[index1][index2] ...' . In server you will receive $_POST['key'] as array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM