1.php 747 B

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