@@ -32,63 +32,49 @@ public function __construct(ContainerInterface $options){
3232 }
3333
3434 /** @inheritdoc */
35- public function request (string $ url , array $ params = null , string $ method = null , $ body = null , array $ headers = null ):HTTPResponseInterface {
35+ protected function getResponse ():HTTPResponseInterface {
36+ $ headers = $ this ->normalizeRequestHeaders ($ this ->requestHeaders );
3637
37- try {
38- $ parsedURL = parse_url ($ url );
39-
40- if (!isset ($ parsedURL ['host ' ]) || $ parsedURL ['scheme ' ] !== 'https ' ){
41- trigger_error ('invalid URL ' );
42- }
43-
44- $ method = strtoupper ($ method ?? 'POST ' );
45- $ headers = $ this ->normalizeRequestHeaders ($ headers ?? []);
46-
47- if (in_array ($ method , ['PATCH ' , 'POST ' , 'PUT ' , 'DELETE ' ]) && is_array ($ body )){
48- $ body = http_build_query ($ body , '' , '& ' , PHP_QUERY_RFC1738 );
49-
50- $ headers ['Content-Type ' ] = 'Content-Type: application/x-www-form-urlencoded ' ;
51- $ headers ['Content-length ' ] = 'Content-length: ' .strlen ($ body );
52- }
53-
54- $ headers ['Host ' ] = 'Host: ' .$ parsedURL ['host ' ].(!empty ($ parsedURL ['port ' ]) ? ': ' .$ parsedURL ['port ' ] : '' );
55- $ headers ['Connection ' ] = 'Connection: close ' ;
56-
57- $ params = $ params ?? [];
58- $ url = $ url .(!empty ($ params ) ? '? ' .http_build_query ($ params ) : '' );
59-
60- $ context = stream_context_create ([
61- 'http ' => [
62- 'method ' => $ method ,
63- 'header ' => $ headers ,
64- 'content ' => $ body ,
65- 'protocol_version ' => '1.1 ' ,
66- 'user_agent ' => $ this ->options ->user_agent ,
67- 'max_redirects ' => 0 ,
68- 'timeout ' => 5 ,
69- ],
70- 'ssl ' => [
71- 'cafile ' => $ this ->options ->ca_info ,
72- 'verify_peer ' => true ,
73- 'verify_depth ' => 3 ,
74- 'peer_name ' => $ parsedURL ['host ' ],
75- 'ciphers ' => 'HIGH:!SSLv2:!SSLv3 ' ,
76- 'disable_compression ' => true ,
77- ],
78- ]);
79-
80- $ response = file_get_contents ($ url , false , $ context );
81- $ responseHeaders = get_headers ($ url , 1 );
82-
83- return new HTTPResponse ([
84- 'headers ' => $ this ->parseResponseHeaders ($ responseHeaders ),
85- 'body ' => trim ($ response ),
86- ]);
38+ if (in_array ($ this ->requestMethod , ['PATCH ' , 'POST ' , 'PUT ' , 'DELETE ' ]) && is_array ($ this ->requestBody )){
39+ $ this ->requestBody = http_build_query ($ this ->requestBody , '' , '& ' , PHP_QUERY_RFC1738 );
8740
41+ $ headers ['Content-Type ' ] = 'Content-Type: application/x-www-form-urlencoded ' ;
42+ $ headers ['Content-length ' ] = 'Content-length: ' .strlen ($ this ->requestBody );
8843 }
89- catch (\Exception $ e ){
90- throw new HTTPClientException ($ e ->getMessage ());
91- }
44+
45+ $ headers ['Host ' ] = 'Host: ' .$ this ->parsedURL ['host ' ].(!empty ($ this ->parsedURL ['port ' ]) ? ': ' .$ this ->parsedURL ['port ' ] : '' );
46+ $ headers ['Connection ' ] = 'Connection: close ' ;
47+
48+ $ url = $ this ->requestURL .(!empty ($ this ->requestParams ) ? '? ' .$ this ->buildQuery ($ this ->requestParams ) : '' );
49+
50+ $ context = stream_context_create ([
51+ 'http ' => [
52+ 'method ' => $ this ->requestMethod ,
53+ 'header ' => $ headers ,
54+ 'content ' => $ this ->requestBody ,
55+ 'protocol_version ' => '1.1 ' ,
56+ 'user_agent ' => $ this ->options ->user_agent ,
57+ 'max_redirects ' => 0 ,
58+ 'timeout ' => 5 ,
59+ ],
60+ 'ssl ' => [
61+ 'cafile ' => $ this ->options ->ca_info ,
62+ 'verify_peer ' => true ,
63+ 'verify_depth ' => 3 ,
64+ 'peer_name ' => $ this ->parsedURL ['host ' ],
65+ 'ciphers ' => 'HIGH:!SSLv2:!SSLv3 ' ,
66+ 'disable_compression ' => true ,
67+ ],
68+ ]);
69+
70+ $ response = file_get_contents ($ url , false , $ context );
71+ $ responseHeaders = get_headers ($ url , 1 );
72+
73+ return new HTTPResponse ([
74+ 'url ' => $ url ,
75+ 'headers ' => $ this ->parseResponseHeaders ($ responseHeaders ),
76+ 'body ' => trim ($ response ),
77+ ]);
9278
9379 }
9480
0 commit comments