Example of using PHP for public platform development: php public
1. Connection to the SAE database.
The host name and port must be used in the future.
@ $ Db = new mysqli (SAE_MYSQL_HOST_M. ':'. SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS, 'Your application name ');
2. XML processing.
All messages sent are in XML format, and the messages you return must also be in XML format. Extract data from XML. SimpleXML is powerful and easy to use. How about packaging XML messages? Save the message template as a string and use sprintf to format the output.
Parse Server POST Data:
// ---------- Receive data ---------- // $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // get POST data // parse post xml data with SimpleXML $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; // obtain the sender's account (OpenID) $ toUsername = $ postObj-> ToUserName; // obtain the recipient's account $ msgType = $ postObj-> MsgType; // message content
Return text message:
Function sendText ($ to, $ from, $ content, $ time) {// return message template $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Content> <! [CDATA [% s]> </Content> <FuncFlag> 0 </FuncFlag> </xml> "; // format the message template $ msgType =" text "; $ time = time (); $ resultStr = sprintf ($ textTpl, $ to, $ from, $ time, $ msgType, $ content); echo $ resultStr ;}
3. Call an API.
There are many API interfaces on the Internet, such as Baidu translation, youdao translation, and weather forecast. You can directly use file_get_contents or curl to call the interfaces, then parse the returned data in xml or json format. SimpleXML and json_decode are convenient for processing. To capture API content, use the re-encapsulated 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 to be crawled curl_setopt ($ ch, CURLOPT_URL, $ url ); // set the cURL parameter. The result must be saved to the string or output to the screen. curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // the waiting time before the connection is initiated, if it is set to 0, you can wait for curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout) infinitely; // run cURL to request the webpage $ File_contents = curl_exec ($ ch); // closes the URL request curl_close ($ ch);} return $ file_contents;} the call of Baidu translation API is as follows: function baiduDic ($ word, $ from = "auto", $ to = "auto") {// urlencode the text to be translated first $ word_code = urlencode ($ word ); // register the API Key $ appid = "yourAPIkey"; // generate the URL of the translation api get address $ 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. Calculate the longitude and latitude of "nearby.
Use the following model to calculate the latitude and longitude of a square. Haversin formula is used.
// $ EARTH_RADIUS = 6371; // Earth radius, the average radius is 6371 km/***, and the four points of the square with a certain distance around a certain latitude and longitude are calculated ** @ param lng float longitude * @ param lat float latitude * @ param distance float radius of the circle where the point is located, this circle is tangent to this square. The default value is 0.5 km * @ return array coordinate of the longitude and latitude 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 (deg 2rad ($ 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 chronological order. The message is a table in the database, the location_X is the dimension, and the location_Y is the longitude: // use this function to calculate the result and bring it 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. Check the string.
It must be 6-20 letters. If the match is true, true is returned. Otherwise, false is returned. Regular expressions are used for matching:
function inputCheck($word) { if(preg_match("/^[0-9a-zA-Z]{6,20}$/",$word)) { return true; } return false; }
6. Obtain a substring from a string containing Chinese characters.Use mb_substr to intercept the http://www.php.net/manual/zh/function.mb-substr.php
7. Check the length of a string mixed with Chinese and English
<? Php $ str = "sanzhi sunchis Development Network"; echo strlen ($ str ). "<br>"; // result: 22 echo mb_strlen ($ str, "UTF8 "). "<br>"; // result: 12 $ strlen = (strlen ($ str) + mb_strlen ($ str, "UTF8")/2; echo $ strlen; // result: 17?>
8. Check for Chinese Characters
<? $ Str = "test Chinese"; echo $ str; echo "
Double byte character encoding range
1. GBK (GB2312/GB18030)
\ X00-\ xff GBK dubyte encoding range
\ X20-\ x7f ASCII
\ Xa1-\ xff Chinese gb2312
\ X80-\ xff Chinese gbk
2. UTF-8 (Unicode)
\ U4e00-\ u9fa5 Chinese
\ X3130-\ x318F Korean
\ XAC00-\ xD7A3 Korean
\ U0800-\ u4e00 Japanese
9. Use of Jquery Mobile
Official Website: http://blog.jquerymobile.com/
It was very painful to write a mobile webpage by myself. CSS debugging was annoying and cross-platform debugging was not good. Later I found this library, which was actually much simpler and looked much more beautiful.
However, some new problems are introduced, such as loading CSS and Javascript on the page, because Jquery Mobile loads the page using Ajax by default and does not refresh the entire html, instead, a page is requested. Therefore, the pages of multiple pages are not fully loaded, and CSS and Javascript in the head are not loaded, therefore, one method is to set ajax = false in the Link property to indicate that the page is not loaded through Ajax, and the other is to load CSS and Javascript into the page. I will not discuss it here.
10. Mobile Web debugging
At first, every time you debug a page, you have to connect your phone to WIFI to refresh the page! Later, I finally learned how...
Recommend this Website: http://www.responsinator.com /? Url = Put Your webpage url in the input box at the top, and then "Go", you can see that your webpage is displayed on various platforms, even Kindle ..
Of course, Google, which is essential for developers, can also act as a mobile browser for us. Press F12 to enter the developer mode and click the setting icon in the lower right corner. You can set the User Agent and Device metrics in Overrides, the results are equally good.