php url data fetch stop on perticular time -
i using php 5.2 , fetching data url using file_get_contents function. loop 5000 , have divided 500 slots , set script this. 500 taking 3 hours complete because url taking time , in 1 sec fine.
what want if url taking more 30 sec skip , go next. want stop fetch after 30 sec.
<?php // create stream context $context = stream_context_create(array( 'http' => array( 'timeout' => 1 // timeout in seconds ) )); // fetch 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 empties if (!empty($contents)) { // woohoo // echo $contents; echo "file fetched"; } else { echo $contents; echo "more 30 sec"; } ?>
i have done not working me because file_get_contents function not stoping continue , thing getting no result after 30 sec time taking sameas u can see in output. output of php
2012-03-09 11:26:38 2012-03-09 11:26:40 more 30 sec
you can set http timeout. (not tested)
<?php $ctx = stream_context_create(array( 'http' => array( 'timeout' => 30 ) )); file_get_contents("http://example.com/", 0, $ctx);
edit: don't know why isn't working code you. if don't manage bring work may want give curl try. faster (but don't know if faster...).
if work you, use curl_setopt function set timeout time curlopt_timeout
flag.
Comments
Post a Comment