Home>Article>PHP Framework> Understand how Thinkphp integrates Douyin SDK

Understand how Thinkphp integrates Douyin SDK

coldplay.xixi
coldplay.xixi forward
2020-08-14 16:44:15 4944browse

Understand how Thinkphp integrates Douyin SDK

Since there are too few tutorials related to Douyin’s official SDK, I wrote this blog in the spirit of the predecessors planting trees for future generations to enjoy the shade

Step one: Download Douyin official SDK

Download address: Douyin SDK official download
SelectPhp download addresscan be downloaded

Related learning recommendations:thinkphp

Step 2: Import the SDK

1. Unzip the downloaded compressed file

2. In the root directory of the thinkphp project Create a newDouyindirectory under theextenddirectory (at the same level as the application directory), and then create a newOpendirectory## under theDouyindirectory.

#3. Copy all the files and folders in the unzipped php-sdk/douyin_open/lib directory to the newly created

Douyin\OpenUnder the directory

After the copy is completed, the directory structure is as follows


##Step 3: Install guzzlehttp dependenciesBecause Douyin’s SDK is basically based on requests sent by

guzzlehttp

, you need to install dependenciesI use

Composer

for installation. If you have not installedComposer, please install it first

1. Execute the command
    composer require guzzlehttp/ guzzle:~6.0
  • 2. After the installation is complete, you can call the interface

Step 4: Interface callInterface calling can refer to the built-in

php-sdk\douyin_open\test\Api

of the downloaded SDK, which contains most of the API calling methods. You can refer to callingI only have Demonstrate authorization and obtain user information

 false])); //填写自己的client_key $client_key = "xxx"; //填写自己的client_secret $client_secret = "xxx"; $grant_type = 'authorization_code';//根据官方文档填写 try { //调用获取AccessToken的接口 $result = $apiInstance->oauthAccessTokenGet($client_key, $client_secret, $code, $grant_type); } catch (Exception $e) { return error("登录失败"); } //判断返回的数据是否为空 if (!$result) { return error("登录失败"); } //判断返回的Message是否为error if ($result->getMessage() == 'error') { return error("登录失败"); } //获取返回数据 $data=$result->getData(); //获取openid和access_toekn $openid = $data->getOpenId(); $access_token = $data->getAccessToken(); //创建用户信息API $userApi = new UserInfoApi(new Client(['verify' => false])); //获取用户信息 $userInfo = $userApi->oauthUserinfoGetWithHttpInfo($access_token,$openid); dump($userInfo); } }

Frequently Asked Questions##1. [0] cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) Error

Cause of the problem

This error is due to Reasons for SSL certificate verification

Solution 1

:

Just change$apiInstance = new DefaultApi(new Client());

to

$apiInstance = new DefaultApi(new Client(['verify'=>false]));That’s it, mainly modify the new Client() here, other APIs are the sameSolution 2: Download a ca-bundle.crt, put it in the corresponding directory, and configure the path in the php.ini filehttps://github.com/bagder/ca-bundle/blob/e9175fec5d0c4d42de24ed6d84a06d504d5e5a09/ca-bundle. crt

Add at the end of php.ini,

curl.cainfo="真实路径/ca-bundle.crt"

Restart the web server

2. Prompt that the configuration is invalid

Call the authorized login interface

https:// open.douyin.com/platform/oauth/connect?client_key=xxx&response_type=code&scope=user_info&redirect_uri=redirect_uri&state=1

Prompt for authorization failure or invalid configuration

Cause of the problem

The application applied for ismobile application

, but the actual call is

web applicationweb scan code. The web application has an authorized domain callback when applying. The configuration is to fill in the callback domain name

Solution

To re-apply for the web application, you need to fill in the callback address


3. Obtain user information report Invalid value for 'e_account_role', must be one of 'EAccountM', 'EAccountS', 'EAccountK'

This error occurs when calling

UserInfoApi

oauthUserinfoGetWithHttpInfointerfaceCause of the problem

Because ## in the user information is returned The #e_account_rolefield is null, which can be solved by modifying the API. This error is generally caused by abnormal calls to the API, such as using the client_key of the mobile application to force authorization of the web application.

Solution plan

Because thee_account_rolefield in the returned user information is null, it can be solved by modifying the API
According to the error prompt of TP, we can send the error report toDouyin\Open\Model \OauthUserinfoResponseData.php line 564
The error occurred atline 564

Comment out lines 563 - 570 to obtain normal user information

Before modification:

After modification:

Related learning recommendations:Programming videos

The above is the detailed content of Understand how Thinkphp integrates Douyin SDK. For more information, please follow other related articles on the PHP Chinese website!

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