Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:16:09+00:00 2026-05-31T09:16:09+00:00

I am using php 5.2 and I am fetching data from url using file_get_contents

  • 0

I am using php 5.2 and I am fetching data from url using file_get_contents function. This is loop for 5000 and I have divided into 500 slots and set a script like this.
For 500 it is taking 3 hours to complete because for some url it is taking too much time and for some it is in 1 sec that is fine.

What I want if url is taking more than 30 sec then skip and go for next.
I want to stop fetch after 30 sec.

    <?php
// Create the stream context
$context = stream_context_create(array(
    'http' => array(
        'timeout' => 1       // Timeout in seconds
    )
));

// Fetch the URL's contents
echo date("Y-m-d H:i:s")."\n";
$contents = file_get_contents('http://example.com', 0, $context);
echo date("Y-m-d H:i:s")."\n";
// Check for empties
if (!empty($contents))
{
    // Woohoo
//    echo $contents;
echo "file fetched";
}
else
{
echo $contents;
echo "more than 30 sec"; 
}
?>

I have already done that it is not working for me because file_get_contents function is not stoping it will continue , then only thing now I am getting no result after 30 sec but time it is taking sameas u can see in output.
Output of php

2012-03-09 11:26:38
2012-03-09 11:26:40
more than 30 sec

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:16:11+00:00Added an answer on May 31, 2026 at 9:16 am

    You can set the HTTP timeout. (Not tested)

    <?php
    $ctx = stream_context_create(array(
        'http' => array(
            'timeout' => 30
         )
    ));
    file_get_contents("http://example.com/", 0, $ctx); 
    

    Source

    Edit: I don’t know why it isn’t working with this code by you. But if you don’t manage it to bring it to work with this you may also want to give CURL a try. This could be eventually also faster for that (but I don’t know if that is really faster…).
    If that would work for you, you could than use the curl_setopt function to set the timeout time with the CURLOPT_TIMEOUT flag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fetching value using jQuery .get() like this $.get('clientarea.php', function(data) { var Val
hi everyone i am fetching some data from a database using php, encoding it
I'm fetching a row of data from a mysql-server using php, then encode it
I'm fetching tabular data from the server using jquery's ajax function. And I'm using
(I am using json with PHP) Hi i have got a url, from some
I have seen examples of fetching data using FQL with the help of PHP
My code fetches CSV data from a PHP page using httplib. When I open
I want to break my string using javascript which is fetching some data from
i am fetching this data from database : $sql = SELECT * FROM articles
I am reading this tutorial on fetching RSS feeds using PHP Simple XML. Reason

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.