Home >Backend Development >PHP Tutorial >How to use PHP to write code to connect to Baidu Intelligent Marketing API

How to use PHP to write code to connect to Baidu Intelligent Marketing API

WBOY
WBOYOriginal
2023-08-15 12:39:191549browse

How to use PHP to write code to connect to Baidu Intelligent Marketing API

How to use PHP to write code to implement docking with Baidu Intelligent Marketing API

With the rapid development of the Internet and the further deepening of the advertising and marketing industry, more and more marketing People and businesses are beginning to use various intelligent tools to improve advertising effectiveness and delivery efficiency. As an important tool, Baidu Intelligent Marketing API can help advertisers and developers realize automated advertising and optimization, thereby improving advertising effectiveness and ROI.

This article will introduce how to use PHP to write code to connect to Baidu Intelligent Marketing API, and provide corresponding code examples.

First of all, we need to apply for and obtain a developer account and application key on the Baidu Intelligent Marketing Open Platform. Then, introduce Baidu's SDK into the code and install it using Composer.

require 'vendor/autoload.php';

use BaiduSDKCommonBaiduClient;
use BaiduSDKCommonBaiduOAuth;

Next, we need to perform OAuth authentication to obtain the access token. This can be achieved through the following code example:

$clientId = 'YOUR_CLIENT_ID';
$clientSecret = 'YOUR_CLIENT_SECRET';
$redirectUri = 'YOUR_REDIRECT_URI';

$baiduOAuth = new BaiduOAuth($clientId, $clientSecret, $redirectUri);
$accessToken = $baiduOAuth->getAccessTokenByCode('YOUR_CODE');

After obtaining the access token, we can use Baidu Intelligent Marketing API to perform advertising operations. Here are some commonly used API examples:

  1. Get all plans
$baiduClient = new BaiduClient($accessToken);
$planService = $baiduClient->getService('PlanService');

$response = $planService->getAllPlans();
  1. Create an ad group
$adgroupService = $baiduClient->getService('AdgroupService');

$adgroup = array(
    'campaignId' => 'YOUR_CAMPAIGN_ID', // 广告计划ID
    'adgroupName' => 'YOUR_ADGROUP_NAME', // 广告组名称
    'maxPrice' => 10.0, // 出价
);

$response = $adgroupService->addAdgroup($adgroup);
  1. Create a promotion creative
$creativeService = $baiduClient->getService('CreativeService');

$creative = array(
    'adgroupId' => 'YOUR_ADGROUP_ID', // 广告组ID
    'title' => 'YOUR_CREATIVE_TITLE', // 创意标题
    'description' => 'YOUR_CREATIVE_DESCRIPTION' // 创意描述
);

$response = $creativeService->addCreative($creative);
  1. Get promotion creative report
$reportService = $baiduClient->getService('ReportService');

$fields = array('creativeId', 'impressions', 'clicks', 'cost');
$report = array(
    'startDate' => '2021-01-01',
    'endDate' => '2021-01-31',
    'levelOfDetails' => 12,
    'performanceData' => implode(',', $fields),
);

$response = $reportService->getProfessionalReport($report);

Through the above sample code, we can realize the basic operations of Baidu Intelligent Marketing API, so as to Automated advertising and optimization.

To sum up, using PHP to write code to connect to Baidu Intelligent Marketing API can help advertisers and developers manage and optimize ads more conveniently. At the same time, Baidu Intelligent Marketing API also provides a wealth of functions and interfaces to meet users with different needs. I believe that with the continuous advancement of technology, Baidu Intelligent Marketing API will play an increasingly important role in the field of advertising and marketing.

The above is the detailed content of How to use PHP to write code to connect to Baidu Intelligent Marketing API. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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