php url data fetch stop on perticular time
<p>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.</p>

<p>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.</p>

<pre><code>    &lt;?php
// Create the stream context
$context = stream_context_create(array(
    'http' =&gt; array(
        'timeout' =&gt; 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"; 
}
?&gt;
</code></pre>

<p>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</p>

<p>2012-03-09 11:26:38
2012-03-09 11:26:40
more than 30 sec</p>
