Thinkphp3.2.3 a common function in the framework, such as encapsulating the Curl function to get interface data

Source: Internet
Author: User

When we need to call the same wrapper function in the control layer, we write the same function several times, it seems that the code is very rambling and not streamlined;

TP Framework has a good mechanism, can be common define a function.php function, when we call the control layer when the direct call can be used, convenient and fast;

The specific implementation of the following is convenient:

We can set up a public function in three more places function.php

(Note: function.php default is not present, need to create manually)

    1. Root directory \application\common\common\function.php (public functions, home and admin can all be called)
    2. Root directory \application\home\common\function.php (home layer public function, can only be called by the home layer)
    3. Root directory \application\admin\common\function.php (admin layer public function, can only be called by admin layer)

Specify where function is stored and where you can choose freely

It is also possible when we do not want to use function-named function.php functions, but it is necessary to manually set the

Here's how:

To add a configuration to the root directory/application/common/conf/config.php:

"Load_ext_file"         = ' vaildata ',//

The vaildate.php file can be loaded automatically at this point.
If there are multiple files that need to be loaded automatically, separate the values of the configuration items with ",", for example:

"Load_ext_file"         = ' vaildata,upload,download ',//

Example: encapsulating the Curl function to get interface data

First step: Create the function.php function in the root directory \application\home\common\;

<?PHP//encapsulating the common curl function    functionCurl$url){        $curl=Curl_init (); curl_setopt ($curl, Curlopt_url,$url); curl_setopt ($curl, Curlopt_returntransfer, 1); curl_setopt ($curl, Curlopt_ssl_verifypeer,false); $data= Curl_exec ($curl); Curl_close ($curl); return $data; }    ?>

Step Two: Call the Curl function on the controller layer that needs to be called;

<?phpnamespace Home\controller; UseThink\controller;classWxjsapicontrollerextendsController { Public functionindex () {//Get Interface Data        $url= ' Fill in the interface address ' here; $data _son= Curl ($url);//Adjust the Curl function in the function.php package        $data= Json_decode ($data _json,true) [' Access_token ']; Var_dump($data);//JSON data Conversion array Form//If you also need to call the Curl function, the same as the direct curl ("interface address") can be;}}? >

The above is an example of the Thinkphp3.2.3 framework to encapsulate the common functions and encapsulate the curl function to obtain the interface data;

Thinkphp3.2.3 a common function in the framework, such as encapsulating the Curl function to get interface data

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.