thinkphp Framework Development Mobile End Interface (1) _php tips

Source: Internet
Author: User
Tags explode mixed

This article has shared the use of the thinkphp framework for the development of Mobile Interface code, the original app to provide API interface, specific content as follows

1. Use the TP framework when placed under the Common folder file name is called function.php

<?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 * @pa Ram array $data specific return information * Function Descript: Returns the JSON array with parameters, flag information, hint information, */function returnapisuccess ($msg = null, $data =
 Array ()) {$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 ' => ' The current 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 Checkdata
  Post ($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;
 }
}

2. 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 (+_+)! ');
  }
 }

3. 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;
 }

4. Data received at app end (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 "
  ]
 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.