123456789101112131415161718192021 |
- <?php
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, "http://example.com");
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); // 设置连接超时时间为3秒
- curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 设置总超时时间为10秒
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- if (curl_errno($ch)) {
- echo 'cURL error: ' . curl_error($ch);
- } else {
- // 获取请求的详细信息
- $info = curl_getinfo($ch);
- echo "Connection time: " . $info['connect_time'] . " seconds\n";
- echo "Total time: " . $info['total_time'] . " seconds\n";
- echo "Name lookup time: " . $info['namelookup_time'] . " seconds\n";
- echo "Pre-transfer time: " . $info['pretransfer_time'] . " seconds\n";
- }
- curl_close($ch);
|