PHP

How to limit by Timeout when opening URL ?

CK CK 3776 
Created at  
455 0 0
Previously, I wrote the example to set timeout for fopen function. But it is not practical way to put timeout, because the configuration data is also modified by that function.

Here's another way to achieve the same function.

$query = "http://kurapa.com/api/?lic=" . $license_key . "&i=" . $ipaddress;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout)
    or die('Can not open connection to server.');
if ($fp)
{
  fputs ($fp, "GET $path HTTP/1.0 Host: " . $host . " ");
  while (!feof($fp))
  {
    $buf .= fgets($fp, 128);
  }
  $lines = split(" ", $buf);
  $data = $lines[count($lines)-1];
  fclose($fp);
} else {
  # enter error handing code here
}
echo $data;


As you can see above, I used fsockopen function to put timeout.
Tags IE PHP auto resize fsockopen Facebook X
Comments 0
Similar posts
  1. How to put timeout when opening URL by fopen ?
    440
  2. How to convert W3C Date Time Format ?
    452
  3. Get the actual filesize for a size above 2GB in Windows
    424
  4. HTML redirection in PHP
    398
  5. ISO 8601 format time format string conversion
    532
  6. How to download a file from a certain URL to server ?
    418
  7. How to extract file name from given URL ?
    441
  8. File downloading via HTTP
    444
  9. How to block a connection from the certain site ?
    348
  10. XMLRPC example
    760
  11. How to upload attachments such as image by metaWeblog API ?
    391
  12. How to post blog content by metaweblog API ?
    484
  13. Posting to a Wordpress Blog Via XMLRPC Using Blogger API
    762
  14. ASCII Artwork Generator from image file
    469
  15. How to delete file on certain path ?
    386
  16. foreach loop
    441
  17. Can PHP switch in strings ?
    389
  18. PHP Switch Example
    426
  19. How to send email in PHP ?
    887
  20. How to pass variables via url
    467
  21. How to declare array in PHP like C/C++
    668
  22. PHP date and time formatting
    427
  23. How to redirect browser in PHP ?
    482
  24. How to get current filename as variable ?
    767
  25. email validation, simple
    411
  26. How to ban / block IP addresses
    507
  27. for loop - simple
    448
  28. form action - detect form post
    427
  29. How to make random number ?
    515
  30. How to force cookies, disable php sessid in URL ?
    552
  1. Unzip a ZIP file in PHP
    498
  2. Uploading files to Amazon S3 with REST API in PHP
    837
  3. Human readable random string for captchas in PHP
    557
  4. How to get browser information in PHP?
    850
  5. How to get the duration between days in PHP
    417
  6. Date format validation in PHP
    570
  7. PHP function converts new line to BR tag
    558
  8. Email Validation Snipet in PHP
    378
  9. PHP socket programming to get content with post method
    407
  10. Get Remote IP Address in PHP
    493
  11. How to remove HTML tags in text string?
    405
  12. Directory listing in PHP
    511
  13. Seconds to String in PHP
    434
  14. How to delete file on certain path ?
    386
  15. ASCII Artwork Generator from image file
    469
  16. How to put timeout when opening URL by fopen ?
    440
  17. How to convert W3C Date Time Format ?
    452
  18. Get the actual filesize for a size above 2GB in Windows
    424
  19. HTML redirection in PHP
    398
  20. ISO 8601 format time format string conversion
    532
  21. How to download a file from a certain URL to server ?
    418
  22. How to extract file name from given URL ?
    441
  23. File downloading via HTTP
    444
  24. XMLRPC example
    760
  25. How to upload attachments such as image by metaWeblog API ?
    391
  26. How to post blog content by metaweblog API ?
    484
  27. Posting to a Wordpress Blog Via XMLRPC Using Blogger API
    762
  28. foreach loop
    441
  29. Can PHP switch in strings ?
    389
  30. PHP Switch Example
    426
Recently updated
  1. How do I create animated background for Google Meet?
    18,998
  2. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    192
  3. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    75
  4. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    151
  5. Array of arguments passed to script in PHP
    83
  6. Invalid Auction Storms in Ad Tech
    91
  7. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    194
  8. The Two Clocks of Ad Server Architecture
    61
  9. Frequency Cap Architecture Overview
    505
  10. Collecting Proof-of-Play from 100,000 Screens
    139
  11. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    276
  12. What is RedisVL?
    102
  13. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    218
  14. TPU & GPU Cluster Management Tools
    229
  15. The Concept of Scaffolding in AI Agents
    217
  16. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    568
  17. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    246
  18. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    321
  19. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    196
  20. Machine Learning: Training vs. Ranking
    200
  21. The Continuous Learning Machine: Designing a Loop That Actually Turns
    212
  22. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    188
  23. Designing a Data Curation System Around a Vector Database
    212
  24. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    228
  25. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    646
  26. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    179
  27. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    203
  28. Understanding Data Lineage
    251
  29. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    159
  30. GPU & LLM Cluster Infrastructure & Task Scheduling Architecture
    229