当 php curl post 时提交的参数为二维数组时,就会提示:Array to string conversion 错误,解决办法是使用 http_build_query 函数处理 post 参数。

$push_api_url = $url;
$post_data = array(
   "type" => "publish",
   "content" => $student->toArray(),
   "to" => $to_uid, 
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $push_api_url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
//curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );//报错,因为$post_data是二维数组
curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query($post_data) );//正常
curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:"));
$return = curl_exec ( $ch );
curl_close ( $ch );
var_export($return);