PHP Public _php Tutorial for a sample of WeChat public platform development using PHP

Source: Internet
Author: User
Tags cdata format message

Examples of public platform development using PHP, PHP public


1. Connection to the SAE database.

Host name and Port are required, and later use is the same.

2.XML of processing.

The message format you send is in XML format, and the message you return must be in XML format as well. Extracting data from XML, using SimpleXML, is powerful and easy to use. What about wrapping it up as an XML message? Save the message template as a string and then format the output with sprintf.

Parse the data for the server POST:

----------Receive data----------//  $postStr = $GLOBALS ["Http_raw_post_data"];//Get POST data  // Parse the post XML data with simplexml $POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);  

Return text message:

function SendText ($to, $from, $content, $time) {   //return message template   $textTpl = "
 
     
  
    %s
  
      
  
    %s
  
      
  
   
    
   %s
  
      
  
    %s
  
      
  
    %s
  
      
 
   
    
   0
  
   
  ";    Format message template   $msgType = "text";   $time = time ();   $RESULTSTR = sprintf ($TEXTTPL, $to, $from,   $time, $msgType, $content);   

3. Invocation of API interface.

There are many API interfaces on the internet, such as Baidu translation, Youdao translation, weather forecast, etc., the call to the interface can be directly with file_get_contents, can also be used to crawl the way of curl, and then according to the format of the return data parsing, is generally XML format or JSON format, It is convenient to use SimpleXML and json_decode when handling. For crawling API content, use the Reseal 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 crawl curl_setopt ($ch, Curlopt_url, $url);      Sets the curl parameter, which requires the result to be saved to a string or output to the screen curl_setopt ($ch, Curlopt_returntransfer, 1);      The time to wait before initiating the connection, and if set to 0, wait indefinitely for curl_setopt ($ch, Curlopt_connecttimeout, $timeout);      Run Curl, request page $file _contents = curl_exec ($ch);   Close URL request curl_close ($ch); } return $file _contents; The Baidu translation API is called as follows: function Baidudic ($word, $from = "Auto", $to = "Auto") {//First urlencode the text to be translated $word _code=urlencod        E ($word);        Registered API Key $appid = "Yourapikey"; Generate the URL get address of the translation 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. Calculation of the latitude and longitude of "nearby".

Use the following model to calculate the latitude and longitude of the square. Adopt Haversin formula.

$EARTH _radius = 6371;//Earth radius, with an average radius of 6371km/** * Calculates four points of a square of a distance around a latitude and longitude * @param LNG float longitude * @param lat float latitude *@ param distance Float The radius of the circle in which the point is located, the circle is tangent to this square, the default value is 0.5 km * @return the latitude and longitude coordinates of the four points of the array 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 (' LA T ' = + $lat + $dlat, ' lng ' = + $lng + $dlng), ' Left-bottom ' =>array (' lat ' + $lat-$dlat, ' LNG ' + $LNG-$dl  ng), ' Right-bottom ' =>array (' lat ' = $lat-$dlat, ' lng ' = + $lng + $dlng));   The query results are sorted in descending order of time, and the message is a table in the database, location_x is a dimension, and location_y is Longitude://Use this function to calculate results and bring in SQL queries.   $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.

A limit of 6-20 letters, which is true if matched, otherwise returns false and is matched with a regular expression:

function Inputcheck ($word) {   if (Preg_match ("/^[0-9a-za-z]{6,20}$/", $word))   {     return true;   }   

6. use MB_SUBSTR to intercept strings with Chinese characters when they are taken http://www.php.net/manual/zh/function.mb-substr.php

7. Detecting mixed string lengths in Chinese and English

<?php    $str = "Three-sunchis Development network";    echo strlen ($STR). "
"; Results: Echo Mb_strlen ($str, "UTF8"). "
"; Results: $strlen = (strlen ($str) +mb_strlen ($str, "UTF8"))/2; echo $strlen; Results: ?>

8. Check if it contains Chinese

Double-byte character encoding range
1. GBK (gb2312/gb18030)
\x00-\xff GBK Double byte 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/
Originally wrote mobile Phone Web page, it is extremely painful, CSS debugging all kinds of annoying, cross-platform is also very bad, later found this library, really simple a lot of, and the interface looks more beautiful.
However, some new problems have been introduced, such as the loading of CSS and Javascript in the page, because Jquery Mobile uses Ajax to load the page, does not refresh the entire HTML, but instead requests a page, so the pages are not fully Loading, the CSS and JavaScript inside the head are not loaded, so one way is to set ajax=false in the properties of the link, indicating that the page is not loaded via Ajax, and that the CSS and JavaScript are loaded in the page. There is no specific discussion here.

10. Mobile Web Debugging
Start every time you debug a page to the phone connected to the WiFi to refresh, simply can not endure! Then finally learned to be good ...
Recommend this site: Http://www.responsinator.com/?url= put their own web page URL in the top of the input box and then "Go", you can see your own web page in each platform to display the effect, even the Kindle has.
Of course, the developer must be Google can also proxy for our mobile browser, press F12 into the developer mode and then click on the bottom right corner of the setting icon, you can set the User agent and Device metrics in Overrides, the effect is also good.

http://www.bkjia.com/PHPjc/1049136.html www.bkjia.com true http://www.bkjia.com/PHPjc/1049136.html techarticle PHP Public 1 For an example of public platform development using PHP. The connection to the SAE database. Host name and Port are required, and later use is the same. @ $db = new Mysqli (Sae_mysql_hos ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.