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
| // ❌ タイムアウトなし
$response = file_get_contents('https://api.example.com/data');
// ✅ タイムアウトを設定
$context = stream_context_create([
'http' => [
'timeout' => 10 // 10秒でタイムアウト
]
]);
$response = file_get_contents('https://api.example.com/data', false, $context);
// ✅ cURL でタイムアウト設定
$ch = curl_init('https://api.example.com/data');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// ✅ Guzzle でタイムアウト設定
$client = new \GuzzleHttp\Client([
'timeout' => 10,
'connect_timeout' => 5
]);
$response = $client->get('https://api.example.com/data');
|