File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ You can also use [Symfony HttpClient documentation](https://symfony.com/doc/curr
14
14
``` php
15
15
$response = $client->get("https://example.com");
16
16
$response = $client->get("https://example.com", ["query" => ["key" => "value"]]);
17
- $response->getContent(); // Get response body
17
+ $response->getContent(); // Get response body, or collection, if response is JSON
18
18
$response->toCollection(); // Transform JSON response to collection
19
19
$response->getStatusCode(); // Get response status code
20
20
$response->getHeaders(); // Get response headers
@@ -29,9 +29,16 @@ $client->delete("https://example.com");
29
29
### Request builder
30
30
You can send your request parameters directly to client methods, but you can also use fluent request builder.
31
31
``` php
32
+ // Add data to request
33
+ $client->query(["key" => "value"])->get("https://example.com")
34
+ $client->body(["key" => "value"])->post("https://example.com")
35
+ $client->json(["key" => "value"])->post("https://example.com")
36
+
32
37
// Add custom headers to request
33
38
$client->headers(["key" => "value"])->get("https://example.com");
39
+
34
40
// Authentication
35
41
$client->auth("auth_basic", ["username", "password"])->get("https://example.com");
36
42
$client->authBasic(["username", "password"])->get("https://example.com");
37
43
$client->authBearer("tokenhere")->get("https://example.com");
44
+ ```
Original file line number Diff line number Diff line change @@ -46,4 +46,22 @@ public function headers($headers)
46
46
$ this ->applyRequestOptions (["headers " => $ headers ]);
47
47
return $ this ;
48
48
}
49
+
50
+ public function body ($ body )
51
+ {
52
+ $ this ->applyRequestOptions (["body " => $ body ]);
53
+ return $ this ;
54
+ }
55
+
56
+ public function json ($ json )
57
+ {
58
+ $ this ->applyRequestOptions (["json " => $ json ]);
59
+ return $ this ;
60
+ }
61
+
62
+ public function query ($ query )
63
+ {
64
+ $ this ->applyRequestOptions (["query " => $ query ]);
65
+ return $ this ;
66
+ }
49
67
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace Ivan770 \HttpClient ;
4
4
5
+ use Symfony \Component \HttpClient \Exception \JsonException ;
6
+
5
7
class Response
6
8
{
7
9
protected $ baseResponse ;
@@ -21,6 +23,15 @@ public function toCollection()
21
23
return collect ($ this ->baseResponse ->toArray ());
22
24
}
23
25
26
+ public function getContent ($ throw = true )
27
+ {
28
+ try {
29
+ return $ this ->toCollection ();
30
+ } catch (JsonException $ exception ) {
31
+ return $ this ->baseResponse ->getContent ($ throw );
32
+ }
33
+ }
34
+
24
35
public function __call ($ name , $ arguments )
25
36
{
26
37
return $ this ->baseResponse ->$ name (...$ arguments );
You can’t perform that action at this time.
0 commit comments