从第一讲进入第二讲,有一个代码实现部份,怎么过渡到代码实现?
public function pushWxMsg(){
//1.获取到access_token
$access_token = '获取到access_token';
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
$array = array(
'touser' => "用户openid", //用户openid
'template_id' => "在公众号下配置的模板id", //在公众号下配置的模板id
'url' => "https://www.imooc.com/course/qa/id/618/t/0?page=1", //点击模板消息会跳转的链接
'data' => array(
'first' => array('value' => "兼职平台签到测试开始", 'color' => "#FF0000"),
'keyword1' => array('value' => '兼职岗位小区门口卖西瓜', 'color' => '#FF0000'), //keyword需要与配置的模板消息对应
'keyword2' => array('value' => time(), 'color' => '#FF0000'), //keyword需要与配置的模板消息对应
'remark' => array('value' => "兼职平台签到测试结束,收到请钉钉回复一下", 'color' => "#FF0000"),
),
);
$json_template = json_encode($array);
// $res = $this->http_curl($url, 'post', 'json', $json_template);
$res = $this->curl_post($url, $json_template);
dump($res);
}
/**
* @param $url
* @param array $data
* @return mixed
* curl请求
*/
function curl_post($url, $data = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}