1+ <?php
2+
3+
4+ namespace LarvaBug \Client ;
5+
6+
7+ use Illuminate \Support \Facades \Session ;
8+
9+ class HttpClient
10+ {
11+ /**
12+ * @var string
13+ */
14+ private $ projectId ;
15+ /**
16+ * @var string
17+ */
18+ private $ projectSecret ;
19+
20+ private const URL = 'http://larvabug.local/api/v1/exception ' ;
21+
22+ /**
23+ * @param string $projectId
24+ * @param string $projectSecret
25+ */
26+ public function __construct (string $ projectId , string $ projectSecret )
27+ {
28+ $ this ->projectId = $ projectId ;
29+ $ this ->projectSecret = $ projectSecret ;
30+ }
31+
32+ /**
33+ * Report error to larvabug website
34+ *
35+ * @param $exception
36+ */
37+ public function report ($ exceptionData )
38+ {
39+ try {
40+ $ data_string = json_encode ($ exceptionData );
41+
42+ $ header = [
43+ 'Content-Type:application/json ' ,
44+ 'Authorization-APP: ' . $ this ->projectId ,
45+ 'Authorization-KEY: ' . $ this ->projectSecret
46+ ];
47+
48+ $ ch = curl_init (self ::URL );
49+
50+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data_string );
51+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ header );
52+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
53+
54+ $ result = curl_exec ($ ch );
55+
56+ curl_close ($ ch );
57+
58+ if ($ result && $ result != 404 ){
59+ Session::put ('lb.lastExceptionId ' , $ result );
60+ }
61+
62+ return true ;
63+ }catch (\Exception $ exception ) {
64+ return false ;
65+ }
66+ }
67+
68+ }
0 commit comments