weixing 微信, 发送消息

本文详细介绍了如何使用PHP结合微信API进行消息发送与预览操作,包括获取access_token、构建POST请求及执行消息发送流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

<meta charset="utf-8">
<?PHP
function getcfg(){
	$cfg['getAccessTokenUrl']='http://xxxx/send_msg.php';
	$cfg['appid']='xxx';
	$cfg['appsecret']='xxx';
	return $cfg;
}
$cfg = getcfg();

$access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$cfg['appid'].'&secret='.$cfg['appsecret'];
$at = file_get_contents($access_token_url);

$at = json_decode($at,true);
 

$post_msg_url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.$at['access_token'];

$post_msg_data = array(
	'touser'=>array('xxx','xxx'), 
	'msgtype'=>'text',
	'text'=>array('content'=>'hello from boxer')
);
$json_msg_data = json_encode($post_msg_data);


//
	$ch = curl_init();
	//设置超时
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	curl_setopt($ch, CURLOPT_URL, $post_msg_url);
	curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
	curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
	curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
	curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
	curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode($json_msg_data));
	//运行curl,结果以jason形式返回
	$data = curl_exec($ch);
	curl_close($ch);


	print_r($data);exit;
//\
 
 
?>

 

 

预览发送内容

 

<?PHP session_start(); error_reporting(0);?>
<meta charset="utf-8">
<?PHP
function getcfg(){
	$cfg['getAccessTokenUrl']='http://xxxxxxxx/send_msg.php';
	$cfg['appid']='xxxxxx';
	$cfg['appsecret']='xxxxx';
	return $cfg;
}
$cfg = getcfg();

$access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$cfg['appid'].'&secret='.$cfg['appsecret'];
$at = file_get_contents($access_token_url);

$at = json_decode($at,true);
 

//$post_msg_url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.$at['access_token'];
$post_msg_url = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token='.$at['access_token'];

$post_msg_data = array(
	'touser'=>'xx-xx-xxxxxxx', 
	'msgtype'=>'text',
	'text'=>array('content'=>'hello from 发士大夫<a href="http://xxxxxx.com">s是s发</a>')
);
$json_msg_data = json_encode($post_msg_data);

$json_msg_data = '{"touser":"oEXy-xxx-xxx","msgtype":"text","text":{"content":"您的优惠券即将过期请尽快使用 \n <a href=\"http://xxxx.com\">点击查看我的优惠券</a>"}}';

//echo urldecode($json_msg_data);exit;


//
	$ch = curl_init();
	//设置超时
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	curl_setopt($ch, CURLOPT_URL, $post_msg_url);
	curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
	curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
	curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
	curl_setopt($ch, CURLOPT_HTTPHEADER, array("content-type: application/x-www-form-urlencoded; charset=UTF-8"));
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
	curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
	curl_setopt($ch, CURLOPT_POSTFIELDS, ($json_msg_data));
	//运行curl,结果以jason形式返回
	$data = curl_exec($ch);
	curl_close($ch);


	print_r($data);exit;
//\
 
 
?>

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值