第一次做微信永久二维码生成,发送请求过后一直提示40052错误,在测试接口上面测试又是完全正确,不知道怎么回事,后来仔细检查了代码过后发现,原来自己post数据的时候忘记把数组转换成json字符串了。json_encode($data_array);转换过后提交就没有问题。
function wx_curl($url, $post_type = "POST", $data_array, &$errorCode = 0, &$errorMsg = "") {
// 初始化$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
if ($post_type == "GET") {
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "GET" );
} else {
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
}
$data_json_str = json_encode($data_array);
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data_json_str);
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
'Content-Type: application/json',
'Content-Length: ' . strlen ( $data_json_str )
) );
// 执行并获取接口传的内容
$ret = curl_exec ( $ch );
curl_close ( $ch );
$returnArray = json_decode ( $ret, true );
if (isset ( $returnArray ["errcode"] )) {
$errorCode = $returnArray ["errcode"];
$wxErrorEnum = include './data/enum/wx/CommonCode.enum.php';
$errorMsg = $wxErrorEnum [$errorCode];
$this->log ( $returnArray );
return null;
}
return $returnArray;
}