1
1
<?php
2
2
namespace CodeClimate \Bundle \TestReporterBundle ;
3
3
4
- use Guzzle \Http \Client ;
5
- use Guzzle \Http \Exception \ClientErrorResponseException ;
6
-
7
4
class ApiClient
8
5
{
9
- protected $ client ;
10
6
protected $ apiHost ;
11
7
12
8
public function __construct ()
13
9
{
14
- $ this ->client = new Client ();
15
10
$ this ->apiHost = "https://codeclimate.com " ;
16
11
17
12
if (isset ($ _SERVER ["CODECLIMATE_API_HOST " ])) {
@@ -22,18 +17,30 @@ public function __construct()
22
17
23
18
public function send ($ json )
24
19
{
25
- $ request = $ this ->client ->createRequest ('POST ' , $ this ->apiHost ."/test_reports " );
26
- $ response = false ;
27
-
28
- $ request ->setHeader ("User-Agent " , "Code Climate (PHP Test Reporter v " .Version::VERSION .") " );
29
- $ request ->setHeader ("Content-Type " , "application/json " );
30
- $ request ->setBody ($ json );
31
-
32
- try {
33
- $ response = $ this ->client ->send ($ request );
34
- } catch (ClientErrorResponseException $ e ) {
35
- $ response = $ e ->getResponse ();
20
+ $ ch = curl_init ();
21
+ curl_setopt_array ($ ch , array (
22
+ CURLOPT_CUSTOMREQUEST => 'POST ' ,
23
+ CURLOPT_URL => $ this ->apiHost .'/test_reports ' ,
24
+ CURLOPT_USERAGENT => 'Code Climate (PHP Test Reporter v ' .Version::VERSION .') ' ,
25
+ CURLOPT_HTTPHEADER => array ('Content-Type: application/json ' ),
26
+ CURLOPT_HEADER => true ,
27
+ CURLOPT_RETURNTRANSFER => true ,
28
+ CURLOPT_POSTFIELDS => (string )$ json ,
29
+ ));
30
+
31
+ $ response = new \stdClass ;
32
+ if ($ raw_response = curl_exec ($ ch )) {
33
+ list ($ response ->headers , $ response ->body ) = explode ("\r\n\r\n" , $ raw_response , 2 );
34
+ $ response ->headers = explode ("\r\n" , $ response ->headers );
35
+ list (, $ response ->code , $ response ->message ) = explode (' ' , $ response ->headers [0 ], 3 );
36
+ }
37
+ else {
38
+ $ response ->code = -curl_errno ($ ch );
39
+ $ response ->message = curl_error ($ ch );
40
+ $ response ->headers = array ();
41
+ $ response ->body = NULL ;
36
42
}
43
+ curl_close ($ ch );
37
44
38
45
return $ response ;
39
46
}
0 commit comments