SemrushApi.php
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace App\Helper;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
/**
* Class SemrushApi
* @package App\Helper
* @author zbj
* @date 2023/5/9
*/
class SemrushApi
{
//接口地址
protected $url = 'https://api.semrush.com';
protected $key = '2927058317c47207e4a8c4cacf10acfd';
/**
* 反链概述
*/
function backlinks_overview($target,$target_type = "root_domain"){
if($target){
$url = $this->url."/analytics/v1/?";
$params = [
"type"=>"backlinks_overview",
"key"=>$this->key,
"target"=>$target,
"target_type"=>$target_type,
"export_columns"=>"ascore,total,domains_num,urls_num,ips_num,ipclassc_num,follows_num,nofollows_num,sponsored_num,ugc_num,texts_num,images_num,forms_num,frames_num"
];
try {
$res = HttpUtils::get($url, $params);
return $this->data($res)[0] ?? [];
}catch (\Exception|GuzzleException $e){
errorLog('获取站点外链数据失败', $params, $e);
return false;
}
}
return [];
}
/**
* 引荐域名
*/
public function backlinks_refdomains($target,$target_type = "root_domain",$offset = 0){
if($target){
$url = $this->url."/analytics/v1/?";
$params = [
"type"=>"backlinks_refdomains",
"key"=>$this->key,
"target"=>$target,
"target_type"=>$target_type,
"export_columns"=>"domain_ascore,domain,backlinks_num,ip,country,first_seen,last_seen",
"display_limit"=>10,
"display_offset"=>$offset
];
try {
$res = HttpUtils::get($url, $params);
return $this->data($res);
}catch (\Exception|GuzzleException $e){
errorLog('获取站点外链数据失败', $params, $e);
return false;
}
}
return [];
}
/**
* 处理结果
* @param $res
* @return array
* @author zbj
* @date 2023/5/9
*/
protected function data($res){
$temp = explode("\n",$res);
$arr =array_map(function ($v){
return explode(";",$v);
}, $temp);
$data = [];
for ($i = 1; $i < count($arr) - 1; $i++) {
$tmp = [];
foreach($arr[0] as $k =>$v){
$tmp[trim($v)] = trim($arr[$i][$k]);
}
$data[] = $tmp;
}
return $data;
}
}