PHP SDK
更新时间:2024-02-28
一、数据接入
1. 物料数据接入
<?php
function post_m($dic) {
global $url, $ak;
$ctx = json_encode($dic, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Gw-Ak: ' . $ak,
'Alias-Name: dd5ym1b8-26'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ctx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = 'https://airec.baidu.com/airec/api/omni/cms/batch-sync';
$ak = '93978aad49c8ce2ceb235dba549d7ae8';
$demo = [
[
"nid" => "nid_1",
"site_name" => "mr.yesterday",
"title" => "杂诗-王维-1",
"summary" => "双燕初命子",
"content" => "双燕初命子,五桃新作花, 来日绮窗前,寒梅著花未。",
"publish_time" => 1648561924764,
"status" => 1,
"tags" => ["杂诗王维", "杂诗"],
"channels" => ["科技", "文学"],
"author" => "王维",
"content_type" => "图文",
"type" => "古诗",
"source" => "1",
"ts" => time() * 1000
]
];
$begin = microtime(true);
$rex = post_m($demo);
$end = microtime(true);
echo "using time: " . round($end - $begin, 6) . ", ret content: " . $rex;
2. 用户数据接入
<?php
function post_m($dic) {
global $url, $ak;
$ctx = json_encode($dic, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Gw-Ak: ' . $ak,
'Alias-Name: dd5ym1b8-26'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ctx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = 'https://airec.baidu.com/airec/api/omni/ums/batch-sync';
$ak = '93978aad49c8ce2ceb235dba549d7ae8';
$demo = [
[
'uid' => 'dd5ym1b8-26_1',
'city' => '北京',
'uid_type' => '1',
'tags' => ['杂诗王维']
]
];
$begin = microtime(true);
$rex = post_m($demo);
$end = microtime(true);
echo "using time: " . round($end - $begin, 6) . ", ret content: " . $rex;
3. 行为数据接入
<?php
function post_m($dic) {
global $url, $ak;
$ctx = json_encode($dic, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Gw-Ak: ' . $ak,
'Alias-Name: dd5ym1b8-26'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ctx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = 'https://airec.baidu.com/airec/api/omni/ims/batch-sync';
$ak = '93978aad49c8ce2ceb235dba549d7ae8';
$demo = [
[
'uid' => 'uid_1',
'nid' => 'nid_1',
'event' => 'show',
'traceid' => '232808585383426618326892988534043392144',
'ts' => round(microtime(true) * 1000)
]
];
$begin = microtime(true);
$rex = post_m($demo);
$end = microtime(true);
echo "using time: " . round($end - $begin, 6) . ", ret content: " . $rex;
二、搜索接口
1. 图文智能搜索
<?php
function post_m($dic) {
global $url, $ak;
$ctx = json_encode($dic, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Gw-Ak: ' . $ak,
'Alias-Name: dd5ym1b8-26'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ctx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = 'https://airec.baidu.com/airec/api/search/main';
$ak = '93978aad49c8ce2ceb235dba549d7ae8';
$body = [
'uid' => 'uid_1',
'query' => '王维',
'sort_field' => 'publish_time', # 按照时间排序,不传默认按照相关度排序
'reverse' => true, # 不传默认true
# 支持的筛选类型:equal,contain,greater_than,less_than,not_equal,not_contain
'filter' => [
'type' => 'and',
'filters' => [
[
'cond' => 'less_than',
'field' => 'publish_time',
'value' => 1748561924765
],
[
'cond' => 'greater_than',
'field' => 'publish_time',
'value' => 1648561924767
],
[
'cond' => 'equal',
'field' => 'source',
'value' => '1'
],
[
'cond' => 'not_equal',
'field' => 'type',
'value' => 'abc'
]
]
]
];
$begin = microtime(true);
$rex = post_m($body);
$end = microtime(true);
echo "using time: " . round($end - $begin, 6) . ", ret content: " . $rex;
2. 搜索联想词
<?php
function post_m($dic) {
global $url, $ak;
$ctx = json_encode($dic, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Gw-Ak: ' . $ak,
'Alias-Name: dd5ym1b8-26'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ctx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = 'https://airec.baidu.com/airec/api/query-suggest';
$ak = '93978aad49c8ce2ceb235dba549d7ae8';
$body = [
'uid' => 'uid_1',
'query' => '王维'
];
$begin = microtime(true);
$rex = post_m($body);
$end = microtime(true);
echo "using time: " . round($end - $begin, 6) . ", ret content: " . $rex;
3. 相关搜索
<?php
function post_m($dic) {
global $url, $ak;
$ctx = json_encode($dic, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Gw-Ak: ' . $ak,
'Alias-Name: dd5ym1b8-26'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ctx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = 'https://airec.baidu.com/airec/api/related-query';
$ak = '93978aad49c8ce2ceb235dba549d7ae8';
$body = [
'query' => 'query_1'
];
$begin = microtime(true);
$rex = post_m($body);
$end = microtime(true);
echo "using time: " . round($end - $begin, 6) . ", ret content: " . $rex;
4. 热门搜索
<?php
function post_m($dic) {
global $url, $ak;
$ctx = json_encode($dic, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Gw-Ak: ' . $ak,
'Alias-Name: dd5ym1b8-26'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ctx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = 'https://airec.baidu.com/airec/api/search/h_search';
$ak = '93978aad49c8ce2ceb235dba549d7ae8';
$body = [
'uid' => 'uid_1',
'type' => '7day',
'req_cnt' => 20,
'page_id' => 1,
'sort_field' => 'click',
'update_rate' => '24hour'
];
$begin = microtime(true);
$rex = post_m($body);
$end = microtime(true);
echo "using time: " . round($end - $begin, 6) . ", ret content: " . $rex;
三、推荐接口
1. 图文个性化推荐
<?php
function post_m($dic) {
global $url, $ak;
$ctx = json_encode($dic, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Gw-Ak: ' . $ak,
'Alias-Name: dd5ym1b8-26'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ctx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = 'https://airec.baidu.com/airec/api/rec/p_rec';
$ak = '93978aad49c8ce2ceb235dba549d7ae8';
$body = [
'uid' => 'uid_1',
// 如果使用频道推荐, 则传入此字段, 同时确保物料中已经传入此字段
// 'channels' => 'tv,科技',
'req_cnt' => 20
];
$begin = microtime(true);
$rex = post_m($body);
$end = microtime(true);
echo "using time: " . round($end - $begin, 6) . ", ret content: " . $rex;
2. 图文相关推荐
<?php
function post_m($dic) {
global $url, $ak;
$ctx = json_encode($dic, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Gw-Ak: ' . $ak,
'Alias-Name: dd5ym1b8-26'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ctx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = 'https://airec.baidu.com/airec/api/rec/r_rec';
$ak = '93978aad49c8ce2ceb235dba549d7ae8';
$body = [
'uid' => 'uid_1',
'nids' => 'nid_1',
'req_cnt' => 20
];
$begin = microtime(true);
$rex = post_m($body);
$end = microtime(true);
echo "using time: " . round($end - $begin, 6) . ", ret content: " . $rex;
3. 图文热门推荐
<?php
function post_m($dic) {
global $url, $ak;
$ctx = json_encode($dic, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Gw-Ak: ' . $ak,
'Alias-Name: dd5ym1b8-26'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ctx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = 'https://airec.baidu.com/airec/api/rec/h_rec';
$ak = '93978aad49c8ce2ceb235dba549d7ae8';
$body = [
'uid' => 'uid_1',
'page_id' => 1,
'req_cnt' => 10
];
$begin = microtime(true);
$rex = post_m($body);
$end = microtime(true);
echo "using time: " . round($end - $begin, 6) . ", ret content: " . $rex;