Development of _php instance of mobile Port interface using thinkphp framework

Source: Internet
Author: User
Tags explode mixed

Program One: Provide API interface to native app

When you use the TP frame, the file name is function.php in the common folder.

<?php/** * Created by ZHANGKX * email:zkx520tnhb@163.com * DATE:2015/8/1 * time:23:15 *//***************** API Development Auxiliary Function **********************//** * @param null $msg return the correct hint information * @param flag Success Curd Operation succeeded * @par Am array $data specific return information * Function Descript: Returns the JSON array with parameters, flags information, hint information, */function returnapisuccess ($msg = null, $data = a
  Rray ()) {$result = array (' flag ' => ' Success ', ' msg ' => $msg, ' data ' => $data);
Print Json_encode ($result); /** * @param null $msg return a hint of a specific error * @param flag Success Curd operation failed * Function descript: Returns the flag information ' Error ' and the JSON number of the hint message
  Group */function returnapierror ($msg = null) {$result = array (' flag ' => ' Error ', ' msg ' => $msg,);
Print Json_encode ($result); /** * @param null $msg return a hint of a specific error * @param flag Success Curd operation failed * Function descript: Return flag info ' ERROR ', and prompt information, current system busy , please try again later;/function Returnapierrorexample () {$result = array (' flag ' => ' Error ', ' msg ' => ' whenThe front system is busy, please try again later!
  ',
  );
Print Json_encode ($result); /** * @param null $data * @return array|mixed|null * Function Descript: Filter post submitted parameters; */Function Checkdatapo
    St ($data = null) {if (!empty ($data)) {$data = explode (', ', $data); foreach ($data as $k => $v) {if (!isset ($_post[$k)) | | (Empty ($_post[$k])) {if ($_post[$k]!==0 && $_post[$k]!== ' 0 ') {returnapierror ($k. ' Value is empty!)
        ');
    }} unset ($data);
    $data = I (' post. ');
    unset ($data [' _url_ '], $data [' token ']);
  return $data; }/** * @param null $data * @return array|mixed|null * Function Descript: Filter get submitted parameters; */Function Checkdataget (
    $data = null) {if (!empty ($data)) {$data = explode (', ', $data); foreach ($data as $k => $v) {if (!isset ($_get[$k)) | | (Empty ($_get[$k])) {if ($_get[$k]!==0 && $_get[$k]!== ' 0 ') {returnapierror ($k. ' Value is empty!)
        ');
    }} unset ($data);
    $data = I (' Get. '); unset ($data [' _urL_ '], $data [' token ']);
  return $data;

 }
}

Query individual Fruit Details

/**
  * Release Module
  * * 
  Get information individual fruit details
  */
  public
  function Getmyreleaseinfo () {
    // Check whether the data
    checkdatapost (' id ') is obtained by post method;
    $where [' id '] = $_post[' id '];
    $field [] = ' id,fruit_name,high_price,low_price,address,size,weight,fruit_pic,remark ';
    $releaseInfo = $this->release_obj->findrelease ($where, $field);
    $releaseInfo [' remark '] = MB_SUBSTR ($releaseInfo [' Remark '],0,49, ' utf-8 '). ' ...';
    Multiple map addresses use commas to intercept strings, and if there is an empty array after interception, you need to filter out
    $releaseInfo [' fruit_pic '] = array_filter (Explode (', '), $releaseInfo [' Fruit_ Pic ']));
    $fruit _pic = $releaseInfo [' Fruit_pic '];unset ($releaseInfo [' fruit_pic ']);
    Add a storage path to a picture
    foreach ($fruit _pic as $k => $v) {
      $releaseInfo [' fruit_pic '] = ' http://'. $_server[' Http_ HOST ']. ' /uploads/release/'. $v;
    }
    if ($releaseInfo) {
      returnapisuccess (", $releaseInfo);
    } else{
      Returnapierror (' Nothing has been found (+_+)! ');
    }
  }

Model of Findrelease () method

/**
  * Query a data */public
  function findrelease ($where, $field) {
    if ($where [' status '] = = ' | | empty ($where [' Status ']) {
      $where [' status '] = Array (' NEQ ', ' 9 ');
    }
    $result = $this->where ($where)->field ($field)->find ();
    return $result;
  }

Data received by app side (after decoding JSON)

{"
  flag": "Success", "Message
  ": "",
  "Responselist": {
    "id": "2",
    "Fruit_name": "Apple",
    "High_price": "8.0", "
    Low_price": "5.0",
    "address": "Tianjin small White House Fruit Market",
    "size": "2.0",
    "Weight": "2.0" , "
    remark": "Urgent need ...",
    "Fruit_pic": [
      "Yun_qi_img/55599e7514815.png",
      "yun_qi_img/ 554f2dc45b526.jpg "
    ]
  }
}

Data received from app side (native JSON string)

Copy Code code as follows:

{"Flag": "Success", "Message": "", "Responselist": {"id": "2", "Fruit_name": "\u82f9\u679c", "High_price": "8.0", "Low_" Price ":" 5.0 "," Address ":" \u5929\u6d25\u5c0f\u767d\u697c\u6c34\u679c\u5e02\u573a "," Size ":" 2.0 "," Weight ":" 2.0 "," Remark ":" \u6025\u9700 ... "," fruit_pic ": [" Http:\/\/fruit.txunda.com\/uploads\/release\/201508\/55599e7514815.png "," Http:\/\/fruit.txunda.com\/uploads\/release\/201508\/554f2dc45b526.jpg "]}}

Scenario Two: In addition, we can also achieve mobile end access through the thinkphp theme template, so you can also do mobile end access

thinkphp template theme mechanism, if only in the PC, as long as the need to modify Default_theme (the new template theme is empty, indicating that the template theme is not enabled) configuration items can facilitate the implementation of multiple template theme switching.

But for the mobile end and PC side, maybe you will design a completely different theme style, and for different antecedents to provide different rendering methods, one of the more popular method is "responsive design", but in my experience, to achieve full "responsive design" is not so easy, and solve the compatibility problem is also a problem, The assumption is that large sites, such as: Taobao, Baidu, Pat these, responsive design must not meet the needs, but the need for mobile phone access users to provide a separate mobile web site.

Thinkphp is completely achievable and fairly simple. And the TPM Intelligent template switching engine, as long as the antecedents of the judge processing can be.

One, add IsMobile () to {Project/common/common.php}

function IsMobile () {//If http_x_wap_profile must be a mobile device if (Isset ($_server[' http_x_wap_profile ')) return true; This article is excerpted from the TPM Intelligent Switch template engine, suitable for TPM development if (Isset ($_server[' http_client ')) && ' phoneclient ' ==$_server[' http_client ']) r
  Eturn true; If the VIA information contains WAP it must be a mobile device, some of the service Chambers block the information if (Isset ($_server[' Http_via ')))//Cannot find the flase, otherwise return to True Stristr ($_server [' Http_via '], ' WAP '?
  True:false; To determine the client flag sent by mobile phone, compatibility needs to be improved if (Isset ($_server[' http_user_agent ')) {$clientkeywords = array (' Nokia ', ' Sony ', ' ERI Csson ', ' mot ', ' Samsung ', ' HTC ', ' SGH ', ' lg ', ' sharp ', ' sie-', ' Philips ', ' Panasonic ', ' Alcatel ', ' Lenovo ', ' iphone ', ' ipod ', ' blackberry ', ' Meizu ', ' Android ', ' NetFront ', ' Symbian ', ' UCWeb ', ' windowsce ', ' palm ', ' operamini ', ' Operamobi ', '
    Openwave ', ' nexusone ', ' cldc ', ' MIDP ', ' wap ', ' mobile '; Find the keyword if (preg_match) ("/") for the mobile browser from Http_user_agent. Implode (' | ', $clientkeywords).
    ")/I", Strtolower ($_server[' http_user_agent '))) {return true; }//Protocol law, because it is possibleInaccurate, put to the last judge if (Isset ($_server[' http_accept ')) {//If only WML is supported and HTML is not supported it must be a mobile device//If WML and HTML are supported but WML is a mobile device before HTML if ((Strpos ($_server[' http_accept '), ' VND.WAP.WML ')!== false) && (Strpos ($_server[' http_accept '), ' text/html ') = = = False | | (Strpos ($_server[' http_accept '], ' vnd.wap.wml ') < Strpos ($_server[' http_accept '], ' text/html ')))
    {return true;
 return false;
 }

Second, in {Project/lib/} to create a commonaction.php, if your project has a public controller, you do not need to create, directly added to the inside.

Class Commonaction extends action{public
  function _initialize () {
    //mobile device browsing, toggle template
    if (IsMobile ()) {
      // Set the default default theme for mobile
      C (' Default_theme ', ' mobile ')
    ;
    //............ More code for you ...
  }
 

Through the above 2 ways to achieve mobile end access, one is the original, one is pseudo native, small partners according to their own project needs to choose.

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.