`

weixing 微信, 发送消息

 
阅读更多

 

 

<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;
//\
 
 
?>

 

 

 

 

 

 

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics