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