Issues (184)

src/FeiYu.php (21 issues)

1
<?php
2
3
namespace FeiYuCRM;
4
5
/**
6
 * FeiYuCRM system class
7
 * @author haosijia <[email protected]>
8
 */
9
class FeiYu
10
{
11
  // Server address
12
  public $host = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
13
  // Data fetch route
14
  public $pull_route = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
15
  // Upload data route
16
  public $push_route = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
17
  // Encryption key
18
  public $signature_key = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
19
  // Token
20
  public $token = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
21
22
  // Timestamp
23
  protected $timestamp = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
24
  // Signature
25
  protected $signature = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
26
  // Start time
27
  protected $start_time = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
28
  // End time
29
  protected $end_time = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
30
  // Page size
31
  protected $page_size = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
32
  // Run route
33
  protected $fetch_route = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
34
  // Data from host
35
  protected $res_data = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
36
  // Push data source
37
  protected $push_data = '';
0 ignored issues
show
Line indented incorrectly; expected at least 4 spaces, found 2
Loading history...
38
39 2
  public function __construct($options)
40
  {
41 2
    $this->host = isset($options['host'])?$options['host']:$this->host;
42 2
    $this->pull_route = isset($options['pull_route'])?$options['pull_route']:$this->pull_route;
43 2
    $this->push_route = isset($options['push_route'])?$options['push_route']:$this->push_route;
44 2
    $this->signature_key = isset($options['signature_key'])?$options['signature_key']:$this->signature_key;
45 2
    $this->token = isset($options['token'])?$options['token']:$this->token;
46 2
    $this->timestamp = time();
47 2
  }
48
49
  /**
50
   * pull data
51
   * @param string $start_time ['Y-m-d']
52
   * @param string $end_time ['Y-m-d']
53
   * @param int $page_size
54
   * @return $this
55
   */
56 1
  public function pullData($start_time, $end_time, $page_size)
57
  {
58 1
    $this->start_time = strtotime($start_time);
59 1
    $this->end_time = strtotime($end_time);
60 1
    $this->page_size = $page_size;
61 1
    $this->fetch_route = $this->pull_route;
62 1
    return $this;
63
  }
64
65
  /**
66
   * push data
67
   * @param array $data
68
   * @return bool
69
   */
70 1
  public function pushData($data)
71
  {
72 1
    if(!isset($data['clue_convert_state']) || !isset($data['clue_id'])){
73
      throw new FeiYuException("Upload data is missing the necessary parameters", 1);
0 ignored issues
show
Line indented incorrectly; expected at least 12 spaces, found 6
Loading history...
74
    }
75 1
    if(!is_numeric($data['clue_convert_state'])){
76
      throw new FeiYuException("'clue_convert_state' must be a numeric type", 1);
0 ignored issues
show
Line indented incorrectly; expected at least 12 spaces, found 6
Loading history...
77
    }
78 1
    $data['clue_convert_state'] = (int)$data['clue_convert_state'];
79 1
    $this->push_data = json_encode([
80 1
      'source' => 0,
81
      'data' => [
82 1
        'clue_id' => $data['clue_id'],
83 1
        'clue_convert_state' => $data['clue_convert_state'],
84
      ],
85
    ]);
86 1
    $this->fetch_route = $this->push_route;
87 1
    $this->fetchCurl();
88
    return !$this->getResData()['status'];
89
  }
90
91
  /**
92
   * get result data from curl
93
   * @return string
94
   */
95
  public function getResData()
96
  {
97
    return $this->res_data;
98
  }
99
100
  /**
101
   * get all page data and run callback function in every page
102
   * @return string
103
   */
104 1
  public function run($callback)
105
  {
106 1
    $page = 1;
107
108
    do {
109 1
      $this->fetchCurl($page);
110
111
      if (call_user_func($callback, $this->res_data['data']) === false) {
112
        return false;
113
      }
114
115
      $page++;
116
    } while (($page-1)*($this->page_size) < $this->res_data['count']);
117
118
    return true;
119
  }
120
121
  /**
122
   * encrypt url and start_time and end_time to signature
123
   * @return $this
124
   */
125 2
  protected function encryptData()
126
  {
127
    // 拼接中的空格很重要
128 2
    if($this->fetch_route == $this->pull_route){
129 1
      $data = $this->fetch_route.'?start_time='.$this->start_time.'&end_time='.$this->end_time.' '.$this->timestamp;
130
    } else {
131 1
      $data = $this->fetch_route.' '.$this->timestamp;
132
    }
133 2
    $this->signature = base64_encode(hash_hmac('sha256', $data, $this->signature_key));
134 2
    return $this;
135
  }
136
137
  /**
138
   * fetch data by curl
139
   * @param string $page
140
   * @return $this
141
   */
142 2
  protected function fetchCurl($page = 1)
143
  {
144 2
    $this->encryptData();
145 2
    $ch = curl_init();
146 2
    if(!$ch){
0 ignored issues
show
$ch is of type false|resource, thus it always evaluated to false.
Loading history...
Line indented incorrectly; expected 8 spaces, found 4
Loading history...
Expected "if (...) {\n"; found "if(...){\n"
Loading history...
There must be a single space between the closing parenthesis and the opening brace of a multi-line IF statement; found 0 spaces
Loading history...
147
      throw new FeiYuException('cURL init failed', 1);
0 ignored issues
show
Line indented incorrectly; expected at least 12 spaces, found 6
Loading history...
148
    }
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 4
Loading history...
149 2
    curl_setopt($ch, CURLOPT_URL, $this->host.$this->fetch_route.'?page='.$page.'&page_size='.$this->page_size.'&start_time='.$this->start_time.'&end_time='.$this->end_time);
150 2
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
151 2
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
152 2
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
153 2
        'Content-Type: application/json;charset=UTF-8',
154 2
        'Signature: ' . $this->signature,
155 2
        'Timestamp: ' . $this->timestamp,
156 2
        'Access-Token: ' . $this->token,
157
    ]);
158 2
    if($this->fetch_route == $this->push_route){
159 1
      curl_setopt($ch, CURLOPT_POST, true);
160 1
      curl_setopt($ch, CURLOPT_POSTFIELDS, $this->push_data);
161
    }
162 2
    $output = curl_exec($ch);
163 2
    $error = curl_error($ch);
164 2
    curl_close($ch);
165 2
    if($error != ''){
166
      throw new FeiYuException($error, 1);
167
    }
168 2
    $this->res_data = json_decode($output, true);
169 2
    if($this->res_data['status'] != 'success'){
170 2
      if(is_array($this->res_data['msg'])){
171
        throw new FeiYuException(json_encode($this->res_data['msg']), 1);
172
      }
173 2
      throw new FeiYuException($this->res_data['msg'], 1);
174
    }
175
    return $this;
176
  }
177
}
178
179
class FeiYuException extends \Exception
180
{
181
  
182
}