在调用微信的接口的时候发现curl_exec返回值为false,代码如下:
// PHP代码
function Post($url, $post_data = '', $timeout = 5){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
if($post_data != ''){
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
$post_data = array(
"touser"=>$openid,
"template_id"=>"kZAWYOudTSHNDysmYzavVuw53pkQGRnwR0To075t1hg",
"url"=>"http://www.genghai.com",
"topcolor"=>"#FF0000",
"data"=>array(
"productType"=>array(
'value'=>'活动名',
"color"=>"#173177",
),
),
);
$post_data = json_encode($post_data);
var_dump(Post($url,$post_data));
执行之后输出的结果:
在Post中添加了curl_error($ch)发现:
SSL验证出了问题,跳过SSL证书验证就好了,在Post中添加:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
OK