generated from farzai/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-usage.php
More file actions
168 lines (135 loc) · 4.24 KB
/
basic-usage.php
File metadata and controls
168 lines (135 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Basic Usage Example
*
* This example demonstrates the basic usage of Transport PHP library.
* It shows how to make simple HTTP requests and handle responses.
*/
require __DIR__.'/../vendor/autoload.php';
use Farzai\Transport\TransportBuilder;
// Create a transport instance with basic configuration
$transport = TransportBuilder::make()
->withBaseUri('https://jsonplaceholder.typicode.com')
->withHeaders([
'Accept' => 'application/json',
'User-Agent' => 'Transport-PHP-Example/1.0',
])
->withTimeout(30)
->build();
echo "=== Transport PHP Basic Usage Examples ===\n\n";
// Example 1: Simple GET Request
echo "1. Simple GET Request\n";
echo str_repeat('-', 50)."\n";
try {
$response = $transport->get('/posts/1')->send();
echo "Status: {$response->statusCode()}\n";
echo "Title: {$response->json('title')}\n";
echo "Body: {$response->json('body')}\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 2: GET Request with Query Parameters
echo "2. GET Request with Query Parameters\n";
echo str_repeat('-', 50)."\n";
try {
$response = $transport->get('/posts')
->withQuery([
'userId' => 1,
'_limit' => 3,
])
->send();
$posts = $response->json();
echo 'Found '.($posts ? count($posts) : 0)." posts:\n";
foreach ($posts as $post) {
echo " - [{$post['id']}] {$post['title']}\n";
}
echo "\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 3: POST Request with JSON Body
echo "3. POST Request with JSON Body\n";
echo str_repeat('-', 50)."\n";
try {
$response = $transport->post('/posts')
->withJson([
'title' => 'My New Post',
'body' => 'This is the content of my new post.',
'userId' => 1,
])
->send();
echo "Status: {$response->statusCode()}\n";
echo "Created Post ID: {$response->json('id')}\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 4: PUT Request (Update)
echo "4. PUT Request (Update)\n";
echo str_repeat('-', 50)."\n";
try {
$response = $transport->put('/posts/1')
->withJson([
'id' => 1,
'title' => 'Updated Title',
'body' => 'Updated body content.',
'userId' => 1,
])
->send();
echo "Status: {$response->statusCode()}\n";
echo "Updated Title: {$response->json('title')}\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 5: DELETE Request
echo "5. DELETE Request\n";
echo str_repeat('-', 50)."\n";
try {
$response = $transport->delete('/posts/1')->send();
echo "Status: {$response->statusCode()}\n";
echo "Successfully deleted\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 6: Handling Non-2xx Responses
echo "6. Handling Non-2xx Responses\n";
echo str_repeat('-', 50)."\n";
try {
$response = $transport->get('/posts/9999')->send();
if ($response->isSuccessful()) {
echo "Post found: {$response->json('title')}\n\n";
} else {
echo "Post not found (Status: {$response->statusCode()})\n\n";
}
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 7: Safe JSON Parsing
echo "7. Safe JSON Parsing with jsonOrNull()\n";
echo str_repeat('-', 50)."\n";
try {
$response = $transport->get('/posts/1')->send();
// This won't throw exception even if JSON is invalid
$data = $response->jsonOrNull();
if ($data !== null) {
echo "Post title: {$data['title']}\n\n";
} else {
echo "Invalid JSON response\n\n";
}
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 8: Working with Response Headers
echo "8. Working with Response Headers\n";
echo str_repeat('-', 50)."\n";
try {
$response = $transport->get('/posts/1')->send();
echo 'Content-Type: '.implode(', ', $response->getHeader('Content-Type'))."\n";
echo "All headers:\n";
foreach ($response->headers() as $name => $values) {
echo " {$name}: ".implode(', ', $values)."\n";
}
echo "\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo "=== Examples Complete ===\n";