In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use PHP to develop Wechat public platform. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Connection to the SAE database.
The hostname and port are required, and the future use is the same.
@ $db = new mysqli (SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS,' your application name')
The treatment of 2.XML.
All messages sent by Wechat are in XML format, and the messages you return must also be in XML format. Extract data from XML and use SimpleXML, which is powerful and easy to use. Packaged as a XML message? Save the message template as a string, and then format the output with sprintf.
Analyze the data of Wechat server POST:
/ /-receive data-/ / $postStr = $GLOBALS ["HTTP_RAW_POST_DATA"]; / / obtain POST data / / use SimpleXML to parse XML data from POST $postObj = simplexml_load_string ($postStr,'SimpleXMLElement',LIBXML_NOCDATA); $fromUsername = $postObj- > FromUserName; / / get sender account (OpenID) $toUsername = $postObj- > ToUserName / / get the recipient account $msgType = $postObj- > MsgType; / / message content
Return a text message:
Function sendText ($to,$from, $content, $time) {/ / return message template $textTpl = "% s 0"; / / format message template $msgType = "text"; $time = time (); $resultStr = sprintf ($textTpl,$to,$from, $time,$msgType,$content); echo $resultStr;}
3. Call the API API.
There are many API interfaces on the Internet, such as Baidu Translation, Youdao Translation, Weather Forecast and so on. You can directly use file_get_contents or curl to grab the interface, and then parse the data according to the format of the returned data, usually in xml format or json format. It is very convenient to use SimpleXML and json_decode when processing. For fetching API content, use the repackaged function:
Function my_get_file_contents ($url) {if (function_exists ('file_get_contents')) {$file_contents = file_get_contents ($url);} else {/ / initialize a cURL object $ch = curl_init (); $timeout = 5; / / set the URL curl_setopt to be crawled ($ch, CURLOPT_URL, $url) / / set the cURL parameter to require the result to be saved in a string or output to curl_setopt on the screen ($ch, CURLOPT_RETURNTRANSFER, 1); / / the time to wait before initiating a connection, if set to 0, then wait indefinitely for curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); / / run cURL and request the web page $file_contents = curl_exec ($ch) / / close the URL request curl_close ($ch);} return $file_contents;} the call to Baidu translation API is as follows: function baiduDic ($word,$from= "auto", $to= "auto") {/ / first perform urlencode processing on the text to be translated $word_code=urlencode ($word); / / registered API Key $appid= "yourAPIkey" / / generate the URL GET address of the translated API $baidu_url = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=".$appid."&q=".$word_code."&from=".$from."&to=".$to; $text=json_decode (my_get_file_contents ($baidu_url)); $text= $text- > trans_result; return $text [0]-> dst;}
4. The calculation of the latitude and longitude of "nearby".
Use the following model to calculate the longitude and latitude of the square. Haversin formula was used.
/ / $EARTH_RADIUS = 6371 / / the radius of the earth, the average radius of 6371km / * calculates the four points of a square at a distance around a longitude and latitude * * @ param lng float longitude * @ param lat float latitude * @ param distance float the radius of the circle that is tangent to the square The default value is 0.5km * @ return array the latitude and longitude coordinates of the four points of the square * / function returnSquarePoint ($lng, $lat,$distance = 0.5) {$EARTH_RADIUS = 6371 $dlng = 2 * asin (sin ($distance/ (2 * $EARTH_RADIUS)) / cos (deg2rad ($lat); $dlng = rad2deg ($dlng); $dlat = $distance/$EARTH_RADIUS; $dlat = rad2deg ($dlat) Return array ('left-top'= > array (' lat'= > $lat + $dlat,'lng'= > $lng-$dlng), 'right-top'= > array (' lat'= > $lat + $dlat,'lng'= > $lng + $dlng), 'left-bottom'= > array (' lat'= > $lat-$dlat,'lng'= > $lng-$dlng), 'right-bottom'= > array (' lat'= > $lat-$dlat,'lng'= > $lng + $dlng)) } sort the query results in descending order of time. Message is a table in the database, location_X is the dimension, and location_Y is the longitude: / / use this function to calculate the results and bring them into the sql query. $squares = returnSquarePoint ($lng, $lat); $query = "select * from message where location_X! = 0 and location_X >". $squares ['right-bottom'] [' lat']. "and location_X
< ".$squares['left-top']['lat'] ."and location_Y >". $squares ['left-top'] [' lng']." And location_Y
< ".$squares['right-bottom']['lng'] ."order by time desc"; 5. 对字符串的检查。 限定为 6-20个字母,符合则返回 true ,否则返回 false,采用正则表达式进行匹配: function inputCheck($word) { if(preg_match("/^[0-9a-zA-Z]{6,20}$/",$word)) { return true; } return false; } 6.对含中文的字符串取子串时,用 mb_substr 进行截取 http://www.php.net/manual/zh/function.mb-substr.php 7.检测中英文混合的字符串长度 8. 检测是否含有中文 双字节字符编码范围 1. GBK (GB2312/GB18030) \x00-\xff GBK双字节编码范围 \x20-\x7f ASCII \xa1-\xff 中文 gb2312 \x80-\xff 中文 gbk 2. UTF-8 (Unicode) \u4e00-\u9fa5 中文 \x3130-\x318F 韩文 \xAC00-\xD7A3 韩文 \u0800-\u4e00 日文 9. Jquery Mobile 的使用 官网:http://blog.jquerymobile.com/ 原来自己写手机网页,真是无比痛苦,CSS 调试各种烦,跨平台也很不好,后来发现了这个库,果然简单了好多,而且界面看起来漂亮多了。 不过也引入了一些新的问题,比如页面内 CSS 和 Javascript 的加载,因为 Jquery Mobile 默认是使用 Ajax 加载页面的,并不会刷新整个 html ,而是请求一个 page 而已,所以对于多个 page 的页面不会完全加载,对于 head 里面的 CSS 和 Javascript 也不会加载,所以一个方法是在链接的属性里设置 ajax=false,指明不通过 Ajax 加载页面,另一个是把 CSS 和 Javascript 的加载放在 page 里面。在这里就不具体谈了。 10. 移动 Web 调试 一开始每次调试个页面都要手机连接 WIFI 去刷新,简直不能忍!后来终于学乖了... 推荐这个网站:http://www.responsinator.com/?url= 把自己的网页 url 放在顶端的输入框里面然后"Go",你就可以看到自己网页在各个平台下了显示效果,连 Kindle 都有.. 当然,开发者必备的谷歌也可以为我们代理成手机浏览器,按 F12 进入开发者模式然后点击右下角的 setting 的图标,可以在 Overrides 里面设置 User Agent 和 Device metrics,效果同样不错。Thank you for reading! This is the end of the article on "how to use PHP for Wechat public platform development". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.