飞鱼CRM数据拉取(PHP重写版)

简介:

CRM即客户关系管理,是指企业用CRM技术来管理与客户之间的关系。在不同场合下,CRM可能是一个管理学术语,可能是一个软件系统。通常所指的CRM,指用计算机自动化分析销售、市场营销、客户服务以及应用等流程的软件系统。它的目标是通过提高客户的价值、满意度、赢利性和忠实度来缩减销售周期和销售成本、增加收入、寻找扩展业务所需的新的市场和渠道。CRM是选择和管理有价值客户及其关系的一种商业策略,CRM要求以客户为中心的企业文化来支持有效的市场营销、销售与服务流程。

而这个飞鱼CRM就是今日头条下的一个客户管理系统,因为飞鱼数据转发原文档使用的是Python示例,所以改写了一下,实现用API将飞鱼CRM的线索数据转发到本地。

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* User: 牧马人
* DateTime: 2020/4/15 15:55
* Desc:飞鱼CRM数据拉取
*/
header("Content-Type: text/html;charset=utf-8");
class FeiYuAction extends HomeCommonAction
{
//飞鱼请求api地址
private $host = 'https://feiyu.oceanengine.com';
private $pull_route = '/crm/v2/openapi/pull-clues/';
//获取几分钟前的数据
private $minute = 5;
//第*页
private $page = 1;
//每页显示*条
private $page_size = 100;
//入口
public function run(){
$this->pp01();
}
//账号为pp01的配置,把signature_key和token换成自己的,可在飞鱼系统=》系统设置=》线索数据转发中获取
private function pp01(){
//飞鱼key
$signature_key = '**********';
//飞鱼token
$token = '******************************';
//开始时间
$start_time = time()-60*$this->minute;
//结束时间
$end_time = time();
//第几页
$page = $this->page;
//每页显示多少数据
$page_size = $this->page_size;
$url = $this->pull_route.'?start_time='.$start_time.'&end_time='.$end_time.' '.time();
$signature = $this->base64($url,$signature_key);
$output = $this->curl($page,$page_size,$start_time,$end_time,$signature,$token);
if ($output['count']>0 && $output['status']=='success'){
$this->insertdata($output['data']);
}
}
/**
* User: 牧马人
* DateTime: 2020/4/15 16:55
* Desc:⽤hmac的sha256对source_data进⾏加密并转为16进制,然后进⾏base64编码生成签名
* @param $data
* @param $signature_key
* @return string
*/
private function base64($data,$signature_key){
$signature = base64_encode(hash_hmac('sha256', $data, $signature_key));
return $signature;
}

/**
* User: 牧马人
* DateTime: 2020/4/15 16:57
* Desc:header参数,自己看去吧
* @param $page
* @param $page_size
* @param $start_time
* @param $end_time
* @param $signature
* @param $token
* @return mixed
*/
private function curl($page,$page_size,$start_time,$end_time,$signature,$token){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->host.$this->pull_route.
'?page='.$page.'&page_size='.$page_size.
'&start_time=' .$start_time.'&end_time='.$end_time);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;charset=UTF-8',
'Signature: ' . $signature,
'Timestamp: ' . time(),
'Access-Token: ' . $token,
));
$output = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
return json_decode($output,true);
}

/**
* User: 牧马人
* DateTime: 2020/4/15 16:59
* Desc:引用BiaoZhao将数据写入到数据库
* @param $data
* @param string $type
*/
private function insertdata($data,$type = ''){
foreach ($data as $value){
$biaoZhaoModel = D('BiaoZhao');
$provincename = substr($value['location'],0,strpos($value['location'], '+'));
$cityname = trim(strrchr($value['location'], '+'),'+');
if (!$province){
$province = 3;
}
$city = M('Cities')->where("name='".$cityname."' and level = 1")->getField('cityid');
if (!$city){
$city = M('Cities')->where("parentid='".$province."' and capital = 1")->getField('cityid');
}
if ($type == 'ntj' && $province==3){
continue;
}
$array['province'] = $province;
$array['city'] = $city;
$array['name'] = $value['name'];
$array['mobilephone'] = $value['telphone'];
$array['content'] = '飞鱼自动导入';
$array['original_time'] = $value['create_time'];
$res = $biaoZhaoModel->saveData(1,$array);
if (!$res){
continue;
}
if ($res['data']['bid']){
$data['utm'] = $value['ad_plan_name'];
$data['dateline'] = time();
$data['id'] = $res['data']['bid'];
$data['idtype'] = 'zhaobiao';
$utmRet = M('Utm')->add($data);
if (!$utmRet){
continue;
}
}
}
}
}