The example in this article describes the implementation method of thinkPHP custom class. Share it with everyone for your reference, the details are as follows:
1. Call through Model
<?php /** * 积分模型 api接口 */ class ApiModel{ private $url = 'http://js.yunlutong.com/Customer/Interface'; public function test() { $post_data['action'] = 'sadf'; $post_data['callback'] = '?'; $res = request_post($this->url, $post_data); $firstChar = substr($res,0,1); if ($firstChar =='?') { $res = substr($res,2); $res = substr($res,0,strlen($res)-1); } elseif($firstChar == '(') { $res = substr($res,1); $res = substr($res,0,strlen($res)-1); } dump(json_decode($res,true)); } }
Do not inherit Model, otherwise an error will be reported because the table does not exist.
call,
$Api = D('Api'); $Api->test();
The call is indeed convenient, but it always feels a bit unreasonable. After all, this D operates the database.
2. Implement it by introducing classes and put the classes under ORG
<?php class Integral{ private $url = 'http://js.yunlutong.com/Customer/Interface'; public function test() { $post_data['action'] = 'sadf'; $post_data['callback'] = '?'; $res = request_post($this->url, $post_data); $firstChar = substr($res,0,1); if ($firstChar =='?') { $res = substr($res,2); $res = substr($res,0,strlen($res)-1); } elseif($firstChar == '(') { $res = substr($res,1); $res = substr($res,0,strlen($res)-1); } dump($res); dump(json_decode($res,true)); } } ?>
Call
import("@.ORG.Api.Integral"); $integralApi = new Integral(); $integralApi->test();
Configure it and load it automatically
'APP_AUTOLOAD_PATH' => '@.ORG,@.ORG.Api',
This makes it convenient to call. No matter how many classes there are in the Api folder, they will be loaded automatically. There is no need to import ("@.ORG.Api.Integral") a single reference.
Readers who are interested in more thinkPHP-related content can check out the special topics on this site: "Introduction to ThinkPHP Tutorial", "Summary of thinkPHP Template Operation Skills", "Summary of Common Methods of ThinkPHP", "Introduction to Codeigniter Tutorial", "CI (CodeIgniter) Framework" Advanced Tutorial", "Basic Tutorial for Getting Started with Smarty Templates" and "Summary of PHP Template Technology".
I hope that what is described in this article will be helpful to everyone’s PHP program design based on the ThinkPHP framework.