php 위 챗 공식 번호 개발 2 급 메뉴
핵심 코드:
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$MsgType=$postObj->MsgType;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content>%s</Content>
<FuncFlag>0</FuncFlag>
</xml>";
// : $user
// : $pwd
// : $host
// : SAE_MYSQL_HOST_S
// : $port
// : $dbname
$dbname = "app_dq095";
$host = "w.rdc.sae.sina.com.cn";
$port = "3306";
$user = "4k514n103z";
$pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";
/* mysql_connect() */
//
$db = mysql_connect($host,$user,$pwd);
if(!$db){
die("Connect Server Failed: " . mysql_error($db));
}
/* mysql_select_db() */
if (!mysql_select_db($dbname)) {
die("Select Database Failed: " . mysql_error($db));
}
mysql_query("set names utf-8",$db);
/* , */
/*!!! , mysql_select_db !!!*/
/* , mysql_connect+mysql_select_db */
/**
* php mysql
*/
if($keyword==" ")
{
$content=" ";
$sql="INSERT INTO `weather`(`id`,`user`,`keyword`) VALUES (NULL,'{$fromUsername}','{$keyword}')";
mysql_query($sql);
}
else
{
$sql="SELECT `keyword` FROM `weather` WHERE `user`= '" . $fromUsername . "'";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
$c= $rs['keyword'];
echo $c;
if($c==" ")
{
$url="http://api.map.baidu.com/telematics/v2/weather?location={$keyword}&ak=1a3cde429f38434f1811a75e1a90310c";
$fa=file_get_contents($url);
$f=simplexml_load_string($fa);
$city=$f->currentCity;
$da1=$f->results->result[0]->date;
$da2=$f->results->result[1]->date;
$da3=$f->results->result[2]->date;
$w1=$f->results->result[0]->weather;
$w2=$f->results->result[1]->weather;
$w3=$f->results->result[2]->weather;
$p1=$f->results->result[0]->wind;
$p2=$f->results->result[1]->wind;
$p3=$f->results->result[2]->wind;
$q1=$f->results->result[0]->temperature;
$q2=$f->results->result[1]->temperature;
$q3=$f->results->result[2]->temperature;
$d1=$city.$da1.$w1.$p1.$q1;
$d2=$city.$da2.$w2.$p2.$q2;
$d3=$city.$da3.$w3.$p3.$q3;
$content=$d1.$d2.$d3;
if (empty($content))
{
$content=" ";}
}
else{
$content=" ";
}
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$content);
echo $resultStr;
index.php 전체 코드 는 다음 과 같 습 니 다.
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$MsgType=$postObj->MsgType;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content>%s</Content>
<FuncFlag>0</FuncFlag>
</xml>";
// : $user
// : $pwd
// : $host
// : SAE_MYSQL_HOST_S
// : $port
// : $dbname
$dbname = "app_dq095";
$host = "w.rdc.sae.sina.com.cn";
$port = "3306";
$user = "4k514n103z";
$pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";
/* mysql_connect() */
//
$db = mysql_connect($host,$user,$pwd);
if(!$db){
die("Connect Server Failed: " . mysql_error($db));
}
/* mysql_select_db() */
if (!mysql_select_db($dbname)) {
die("Select Database Failed: " . mysql_error($db));
}
mysql_query("set names utf-8",$db);
/* , */
/*!!! , mysql_select_db !!!*/
/* , mysql_connect+mysql_select_db */
/**
* php mysql
*/
if($keyword==" ")
{
$content=" ";
$sql="INSERT INTO `weather`(`id`,`user`,`keyword`) VALUES (NULL,'{$fromUsername}','{$keyword}')";
mysql_query($sql);
}
else
{
$sql="SELECT `keyword` FROM `weather` WHERE `user`= '" . $fromUsername . "'";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
$c= $rs['keyword'];
echo $c;
if($c==" ")
{
$url="http://api.map.baidu.com/telematics/v2/weather?location={$keyword}&ak=1a3cde429f38434f1811a75e1a90310c";
$fa=file_get_contents($url);
$f=simplexml_load_string($fa);
$city=$f->currentCity;
$da1=$f->results->result[0]->date;
$da2=$f->results->result[1]->date;
$da3=$f->results->result[2]->date;
$w1=$f->results->result[0]->weather;
$w2=$f->results->result[1]->weather;
$w3=$f->results->result[2]->weather;
$p1=$f->results->result[0]->wind;
$p2=$f->results->result[1]->wind;
$p3=$f->results->result[2]->wind;
$q1=$f->results->result[0]->temperature;
$q2=$f->results->result[1]->temperature;
$q3=$f->results->result[2]->temperature;
$d1=$city.$da1.$w1.$p1.$q1;
$d2=$city.$da2.$w2.$p2.$q2;
$d3=$city.$da3.$w3.$p3.$q3;
$content=$d1.$d2.$d3;
if (empty($content))
{
$content=" ";}
}
else{
$content=" ";
}
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$content);
echo $resultStr;
}else {
echo "";
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Laravel - 변환된 유효성 검사 규칙으로 API 요청 제공동적 콘텐츠를 위해 API를 통해 Laravel CMS에 연결하는 모바일 앱(또는 웹사이트) 구축을 고려하십시오. 이제 앱은 CMS에서 번역된 콘텐츠를 받을 것으로 예상되는 다국어 앱이 될 수 있습니다. 일반적으로 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.