通知php从mssql数据库提取数据推送到企业客户端。
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<?php
function curlPost($url,$data=""){
$ch = curl_init();
$opt = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 20
);
$ssl = substr($url,0,8) == "https://" ? TRUE : FALSE;
if ($ssl){
$opt[CURLOPT_SSL_VERIFYHOST] = 1;
$opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
}
curl_setopt_array($ch,$opt);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$corpid="ww0de32523775685685"; //企业微信CorpID
$secret="vDob_AXmpaZf0qOStFdkteCpgGCXPNYdr5265485";//微信企业应用密钥报账通知
$Url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$secret";//获取访问当前应用的令牌 access_token
$res = curlPost($Url);
$ACCESS_TOKEN=json_decode($res)->access_token;
$Url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$ACCESS_TOKEN";
$bzbh=$_GET['bzbh'];
$sqr=$_GET['sqr'];
$lb=$_GET['lb'];
$server="192.168.10.2";
$conn = sqlsrv_connect( $server, array( 'Database' => 'kaiping','UID'=>'sa', 'PWD'=>'admin1234', 'Database'=>'kqmanager', 'CharacterSet'=>'utf-8' ) );
$stmt = sqlsrv_query( $conn, "select CONVERT(VARCHAR(10), a.ykrq, 120) yk_rq, a.*,b.bh as sqrbh,c.bh as sprbh,d.bh as cwrybh FROM kpmaster AS a LEFT OUTER JOIN bzqx AS b ON a.xm = b.xm LEFT OUTER JOIN kpqx AS c ON a.spr = c.xm LEFT OUTER JOIN kqqx AS d ON a.cwry = d.xm where a.grbh=$bzbh and a.xm='".$sqr."'" , array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
if($conn)
{
echo "●数据库连接成功!";
}
else
{
echo "●数据库连接失败,请联系管理员!!!";
exit(0);
}
if($stmt=== false)
{
echo "<br>●数据表连接失败!";
exit(0);
}
$row_count = sqlsrv_num_rows( $stmt );
if (($row_count === false) || ($row_count ==0))
{
echo '<br>●没有查询到需要发送的记录!!!';
exit(0);
}
else if ($row_count >0)
{
echo $lb."<br>";
if ($lb==0)
$bh=$row["sqrbh"];
else if ($lb==1)
$bh=$row["sprbh"];
else if ($lb==2)
$bh=$row["cwrybh"];
else
{
echo '<br>●参数错误!!!';
exit(0);
}
$msg= '你有一条信息来自<br>【资金管理系统】: ';
$msg= $msg."<br>=====================";
$msg= $msg."<br> 申请人:".$row["xm"];
$msg= $msg."<br> 报账类型:".$row["fyyt"];
$msg= $msg."<br> 费用类型:".$row["bzlb"];
$msg= $msg."<br> 支付方式:".$row["zffs"];
if ($row["zffs"]==="银行")
{
$msg= $msg."<br> 收款方:".$row["skr"];
}
$msg= $msg."<br> 金额:".$row["bzje"].'元';
$msg= $msg."<br> 备注:".$row["bz"];
$msg= $msg."<br>=====================";
if(!empty($row["jjcd"]))
{
if ($row["jjcd"]==="非常紧急")
{
$msg= $msg.'<br> 紧急程度:'.$row["jjcd"];
$msg= $msg.'<br> 用款日期:'.$row["yk_rq"];
}
if ($row["jjcd"]==="紧急")
{
$msg= $msg.'<br> 紧急程度:'.$row["jjcd"];
if(!empty($row["yk_rq"]))
{
$msg= $msg.'<br> 用款日期:'.$row["yk_rq"];
}
}
}
$msg= $msg."<br> 状态:".$row["spzt"];
$msg= $msg."<br> 审批人:".$row["spr"];
$msg= $msg."<br><br>系统发送于:<br> ".date("Y-m-d H:i:s");
$data="{
\"touser\": \"$bh\",
\"msgtype\":\"text\",
\"agentid\":1000010,
\"text\":{\"content\":\"$msg\" },
\"safe\":0
}";
$res = curlPost($Url,$data);
$errmsg=json_decode($res)->errmsg;
if($errmsg==="ok")
{
echo "<br>"." ".$bzbh."发送成功!";
}
else
{
echo "<br>"." ".$bzbh."发送失败!";
}
}
//发送模块线束
sqlsrv_close($conn);
评论