1
1
<?php
2
2
3
- define ('SENSORS_ANALYTICS_SDK_VERSION ' , '1.10.7 ' );
3
+ define ('SENSORS_ANALYTICS_SDK_VERSION ' , '1.10.9 ' );
4
4
5
5
class SensorsAnalyticsException extends \Exception {
6
6
}
@@ -530,15 +530,15 @@ public function clear_super_properties() {
530
530
*
531
531
*/
532
532
public function flush () {
533
- $ this ->_consumer ->flush ();
533
+ return $ this ->_consumer ->flush ();
534
534
}
535
535
536
536
/**
537
537
* 在进程结束或者数据发送完成时,应当调用此接口,以保证所有数据被发送完毕。
538
538
* 如果发生意外,此方法将抛出异常。
539
539
*/
540
540
public function close () {
541
- $ this ->_consumer ->close ();
541
+ return $ this ->_consumer ->close ();
542
542
}
543
543
544
544
/**
@@ -773,24 +773,49 @@ class BatchConsumer extends AbstractConsumer {
773
773
private $ _max_size ;
774
774
private $ _url_prefix ;
775
775
private $ _request_timeout ;
776
+ private $ file_handler ;
776
777
777
778
/**
778
779
* @param string $url_prefix 服务器的 URL 地址。
779
780
* @param int $max_size 批量发送的阈值。
780
781
* @param int $request_timeout 请求服务器的超时时间,单位毫秒。
782
+ * @param boolean $response_info 发送数据请求是否返回详情 默认 false。
783
+ * @param string $filename 发送数据请求的返回状态及数据落盘记录,必须同时 $response_info 为 ture 时,才会记录。
781
784
*/
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 ) {
783
786
$ this ->_buffers = array ();
784
787
$ this ->_max_size = $ max_size ;
785
788
$ this ->_url_prefix = $ url_prefix ;
786
789
$ 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
+ }
787
798
}
799
+
788
800
789
801
public function send ($ msg ) {
790
802
$ this ->_buffers [] = $ msg ;
791
803
if (count ($ this ->_buffers ) >= $ this ->_max_size ) {
792
804
return $ this ->flush ();
793
805
}
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
+ }
794
819
return true ;
795
820
}
796
821
@@ -801,7 +826,7 @@ public function flush() {
801
826
$ ret = $ this ->_do_request (array (
802
827
"data_list " => $ this ->_encode_msg_list ($ this ->_buffers ),
803
828
"gzip " => 1
804
- ));
829
+ ), $ this -> _buffers );
805
830
}
806
831
if ($ ret ) {
807
832
$ this ->_buffers = array ();
@@ -815,7 +840,7 @@ public function flush() {
815
840
* @param array $data
816
841
* @return bool 请求是否成功
817
842
*/
818
- protected function _do_request ($ data ) {
843
+ protected function _do_request ($ data, $ origin_data ) {
819
844
$ params = array ();
820
845
foreach ($ data as $ key => $ value ) {
821
846
$ params [] = $ key . '= ' . urlencode ($ value );
@@ -840,6 +865,20 @@ protected function _do_request($data) {
840
865
841
866
$ ret = curl_exec ($ ch );
842
867
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
+ }
843
882
if (false === $ ret ) {
844
883
curl_close ($ ch );
845
884
return false ;
@@ -870,6 +909,10 @@ private function _gzip_string($data) {
870
909
}
871
910
872
911
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 ;
874
917
}
875
918
}
0 commit comments