Why is PHP Curl messing up multidimentional array? -
i've been stuck trying debug .. im passing multidimensional array script via curl, , wanted array after passing curl execution .. seems after array passed through curl (a copy of @ least), original array gets messed up!
$myarray[0]['name'] = "tj"; $myarray[0]['age'] = "21"; $myarray[0]['sex'] = "yes please"; printr($myarray); //outputs array //upload array data as-is central // curl resource $curl = curl_init(); // set options curl curl_setopt_array($curl, array( curlopt_returntransfer => 1, curlopt_url => 'http://localhost/projects/sync_dtr.php?', curlopt_useragent => 'tk local code: blq ', curlopt_post => 1, curlopt_postfields => $myarray )); // send request & save response $resp $response = curl_exec($curl); // close request clear resources curl_close($curl); printr($myarray); //doesn't output array properly, instead outputs "array ( [0] => array ) "
is bug or expected result/effect? don't understand how curl wrote/modified original array. heck if make copy of $myarray , , use 1 copy in curl , bot copies messed up!!
but if don't use multidimensional array, seems fine.
what happens internally each top array element turned string; unfortunately, performed without applying copy-on-write semantics. won't go far calling bug, it's unintuitive.
that said, curlopt_postfields
shouldn't used multi-dimensional values; instead, use http_build_query()
:
curl_setopt($curl, curlopt_postfields, http_build_query(array( 'myarray' => $myarray )));
btw, i've made sure top level element string; kind of necessary url-encoded form values.
Comments
Post a Comment