Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54b4f09

Browse files
committed
batch 模式新增请求详情返回
1 parent b3e01e5 commit 54b4f09

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

.DS_Store

6 KB
Binary file not shown.

SensorsAnalytics.php

+50-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
define('SENSORS_ANALYTICS_SDK_VERSION', '1.10.7');
3+
define('SENSORS_ANALYTICS_SDK_VERSION', '1.10.9');
44

55
class SensorsAnalyticsException extends \Exception {
66
}
@@ -530,15 +530,15 @@ public function clear_super_properties() {
530530
*
531531
*/
532532
public function flush() {
533-
$this->_consumer->flush();
533+
return $this->_consumer->flush();
534534
}
535535

536536
/**
537537
* 在进程结束或者数据发送完成时,应当调用此接口,以保证所有数据被发送完毕。
538538
* 如果发生意外,此方法将抛出异常。
539539
*/
540540
public function close() {
541-
$this->_consumer->close();
541+
return $this->_consumer->close();
542542
}
543543

544544
/**
@@ -773,24 +773,49 @@ class BatchConsumer extends AbstractConsumer {
773773
private $_max_size;
774774
private $_url_prefix;
775775
private $_request_timeout;
776+
private $file_handler;
776777

777778
/**
778779
* @param string $url_prefix 服务器的 URL 地址。
779780
* @param int $max_size 批量发送的阈值。
780781
* @param int $request_timeout 请求服务器的超时时间,单位毫秒。
782+
* @param boolean $response_info 发送数据请求是否返回详情 默认 false。
783+
* @param string $filename 发送数据请求的返回状态及数据落盘记录,必须同时 $response_info 为 ture 时,才会记录。
781784
*/
782-
public function __construct($url_prefix, $max_size = 50, $request_timeout = 1000) {
785+
public function __construct($url_prefix, $max_size = 50, $request_timeout = 1000, $response_info = false, $filename = false) {
783786
$this->_buffers = array();
784787
$this->_max_size = $max_size;
785788
$this->_url_prefix = $url_prefix;
786789
$this->_request_timeout = $request_timeout;
790+
$this->_response_info = $response_info;
791+
try {
792+
if($filename !== false && $this->_response_info !== false) {
793+
$this->file_handler = fopen($filename, 'a+');
794+
}
795+
} catch (\Exception $e) {
796+
echo $e;
797+
}
787798
}
799+
788800

789801
public function send($msg) {
790802
$this->_buffers[] = $msg;
791803
if (count($this->_buffers) >= $this->_max_size) {
792804
return $this->flush();
793805
}
806+
// data into cache buffers,back some log
807+
if($this->_response_info){
808+
$result = array(
809+
"ret_content" => "data into cache buffers",
810+
"ret_origin_data" => "",
811+
"ret_code" => 900,
812+
);
813+
if ($this->file_handler !== null) {
814+
// need to write log
815+
fwrite($this->file_handler, stripslashes(json_encode($result)) . "\n");
816+
}
817+
return $result;
818+
}
794819
return true;
795820
}
796821

@@ -801,7 +826,7 @@ public function flush() {
801826
$ret = $this->_do_request(array(
802827
"data_list" => $this->_encode_msg_list($this->_buffers),
803828
"gzip" => 1
804-
));
829+
),$this->_buffers);
805830
}
806831
if ($ret) {
807832
$this->_buffers = array();
@@ -815,7 +840,7 @@ public function flush() {
815840
* @param array $data
816841
* @return bool 请求是否成功
817842
*/
818-
protected function _do_request($data) {
843+
protected function _do_request($data,$origin_data) {
819844
$params = array();
820845
foreach ($data as $key => $value) {
821846
$params[] = $key . '=' . urlencode($value);
@@ -840,6 +865,20 @@ protected function _do_request($data) {
840865

841866
$ret = curl_exec($ch);
842867

868+
// judge back detail response
869+
if($this->_response_info){
870+
$result = array(
871+
"ret_content" => $ret,
872+
"ret_origin_data" => $origin_data,
873+
"ret_code" => curl_getinfo($ch, CURLINFO_HTTP_CODE),
874+
);
875+
if ($this->file_handler !== null) {
876+
// need to write log
877+
fwrite($this->file_handler, stripslashes(json_encode($result)) . "\n");
878+
}
879+
curl_close($ch);
880+
return $result;
881+
}
843882
if (false === $ret) {
844883
curl_close($ch);
845884
return false;
@@ -870,6 +909,10 @@ private function _gzip_string($data) {
870909
}
871910

872911
public function close() {
873-
return $this->flush();
912+
$closeResult = $this->flush();
913+
if ($this->file_handler !== null) {
914+
fclose($this->file_handler);
915+
}
916+
return $closeResult;
874917
}
875918
}

0 commit comments

Comments
 (0)