Home PHP Framework ThinkPHP Understand how Thinkphp integrates Douyin SDK

Understand how Thinkphp integrates Douyin SDK

Aug 14, 2020 pm 04:44 PM
sdk thinkphp

<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/202008/14/2020081416430198815.jpg" class="lazy" 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.

#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 installed Composer, 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 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=&#39;&#39;)
  {
    //创建默认API的实例,verify=false不做ssl验证,否则可能报错,根据实际情况而定
    $apiInstance = new DefaultApi(new Client([&#39;verify&#39; => false]));
    //填写自己的client_key
    $client_key = "xxx";
    //填写自己的client_secret
    $client_secret = "xxx";
    $grant_type = &#39;authorization_code&#39;;//根据官方文档填写
    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() == &#39;error&#39;) {
      return error("登录失败");
    }
    //获取返回数据
    $data=$result->getData();
    //获取openid和access_toekn
    $openid = $data->getOpenId();
    $access_token = $data->getAccessToken();
    //创建用户信息API
    $userApi = new UserInfoApi(new Client([&#39;verify&#39; => false]));
    //获取用户信息
    $userInfo = $userApi->oauthUserinfoGetWithHttpInfo($access_token,$openid);
    dump($userInfo);

  }
}
Copy after login

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 file https://github.com/bagder/ca-bundle/blob/e9175fec5d0c4d42de24ed6d84a06d504d5e5a09/ca-bundle. crt

Add at the end of php.ini,

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

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 is mobile 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

oauthUserinfoGetWithHttpInfo interfaceCause 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles