如何通过cURL发送一个数组,以便它可以直接存储在数据库中


How to send an array via post in cURL so that it can be store in Database directly as it is

我正在使用CI,其中我通过post发送数组。

我把它转换成像

这样的字符串
$post_string= "";
foreach ($post_value as $k=>$v){
    $post_string = $post_string.$k.'='.$v.'&';
}
$post_string = rtrim($post_string, "&");
$post_string = trim($post_string, "&");

现在我在curl请求

中传递它
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

这样的。我想把它保存到数据库中,然后再次我必须使它成为一个数组,以便我可以发送它模型。

如果数组包含多个元素,则此方法将变为冗长。

是否有办法通过post直接发送这个数组?

你可以试试:

$json = json_encode($post_value);
$ch = curl_init('http://put_the_url_here');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($json))                                                                       
); 
$result = curl_exec($ch);

希望这对你有帮助!

你可以序列化数组然后传递。后来. .