Home > Article > PHP Framework > Understand how Thinkphp integrates Douyin SDK
<img src="https://img.php.cn/upload/article/202008/14/2020081416430198815.jpg" alt="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
Select Php download address
can 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 new Douyin
directory under the extend
directory (at the same level as the application directory), and then create a new Open
directory## under the Douyin
directory.
Douyin\OpenUnder the directory
##Step 3: Install guzzlehttp dependenciesBecause Douyin’s SDK is basically based on requests sent by
guzzlehttp, you need to install dependenciesI use
for installation. If you have not installed Composer
, please install it first
2. After the installation is complete, you can call the interfaceStep 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 calling I only have Demonstrate authorization and obtain user information
<?php /** * Created by PhpStorm. * User: bigniu * Date: 2020-04-25 * Time: 21:46:42 */ namespace app\api\controller\v1; use Douyin\Open\Api\DefaultApi; use Douyin\Open\Api\UserInfoApi; use Exception; use GuzzleHttp\Client; use think\Controller; class Douyin extends Controller { public function auth($code='') { //创建默认API的实例,verify=false不做ssl验证,否则可能报错,根据实际情况而定 $apiInstance = new DefaultApi(new Client(['verify' => 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 same
Solution 2: Download a ca-bundle.crt, put it in the corresponding directory, and configure the path in the php.ini file
https://github.com/bagder/ca-bundle/blob/e9175fec5d0c4d42de24ed6d84a06d504d5e5a09/ca-bundle. crt
curl.cainfo="真实路径/ca-bundle.crt"
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 configurationCause of the problem
The application applied for is mobile application
, but the actual call isweb 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 callingUserInfoApi
oauthUserinfoGetWithHttpInfo interface
Cause of the problem
Because ## in the user information is returned The #e_account_role field 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 the e_account_role
field 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 to Douyin\Open\Model \OauthUserinfoResponseData.php line 564
The error occurred at line 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!