Home  >  Article  >  Backend Development  >  How to better call API interface in PHP

How to better call API interface in PHP

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-06-22 16:55:103180browse

This article will introduce to you a better way to call the API interface in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to better call API interface in PHP

API interfaces have been very commonly used in various scenarios. The API interface is usually called in the PHP background and needs to be encapsulated through the Curl library, not to mention various recharge thresholds. , but also confused by parameters such as appKey and appSecret of various api interface platforms, and cannot be called uniformly. The ThinkAPI service officially produced by ThinkPHP is precisely to solve various troublesome problems in PHP interface calling.

ThinkAPI unified API interface service is a set of interface calling services and SDKs encapsulated by official joint partners. It is designed to help ThinkPHP developers call various APIs provided by official and third parties more conveniently and at a lower cost. Interfaces and services to better build a developer ecosystem.

The API interface can be called in a more elegant way through the SDK function provided by ThinkAPI. First, you need to install the think-api library in your project (applicable to any PHP5.6 project, without any framework requirements).

composer require topthink/think-api

Then you can call the interface you need to query and return data. It supports all API interfaces of ThinkAPI. Take the interface for querying the region where the ID card belongs as an example:

use thinkapiClient;

$client = new Client("appCode");

$result = $client->idcardIndex()
    ->withCardno('身份证号码')
    ->request();

The idcardIndex method is called The withCardno method of the ID card ownership query interface indicates that the cardno parameter is passed in. If more parameters need to be passed in, more methods can be called in a chain. Finally, the actual call is made through the request method and the data is returned. If you use the IDE, you don't need to remember any interface method names and parameter method names, they will be automatically prompted.

All API calling services of ThinkAPI must set the appCode value (you only need to register an account to obtain it), which is used for identity authentication of interface calls. If you need to call it multiple times, it is recommended that you encapsulate a helper function in the project, for example:

use thinkapiClient;

/**
 * API接口调用助手函数
 * @return Client
 */
function api(): Client
{
    return new Client('yourAppCode');
}

// 调用示例
$result = api()->idcardIndex()
    ->withCardno('身份证号码')
    ->request();

All interface services and methods support IDE automatic prompting and completion (please be sure to note that the method case must be consistent) , all returned data is in JSON format, so basically no documentation is needed to complete the interface development work. Some common problems in API interface calls can be avoided through system method encapsulation. You don't even need to care whether the interface uses GET or POST, it is all handled automatically by the system.

SDK encapsulates all interfaces and parameters into independent methods. You can call any officially supported API interface as simply as calling a class method, and there is no need to remember the details of each interface. What are the parameters?

If your environment does not support Composer or the PHP version is too low, you may need to encapsulate the Curl library yourself to call the interface. ThinkAPI interface documents provide two methods of calling: directly calling the interface address and calling using the SDK.

For more detailed usage, please refer to: https://docs.topthink.com/think-api

Recommended learning: php video tutorial

The above is the detailed content of How to better call API interface in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete