-
-
$opts = array( - 'http'=>array(
- 'method'=>"GET",
- 'timeout'=>1,/ /Unit second
- )
- );
$cnt=0;
- while($cnt<3 && ($bb=file_get_contents("http://bbs.it-home.org" , false, stream_context_create($opts)))===FALSE) $cnt++;
- echo $cnt;
- echo $bb;
-
Copy code
2. If there is a delay, then Just try a few more times
Sometimes the failure is caused by network and other factors.
You can modify the program to retry several times when it fails, and then give up if it still fails, because file_get_contents() will return FALSE if it fails.
For example:
$cnt=0;
while($cnt"GET", can it be set to post?
For example:
-
-
function Post($url, $post = null){ - $context = array ();
- if (is_array ( $post )) {
- ksort ( $ post );
- $context ['http'] = array (
- 'timeout' => 60,
- 'method' => 'POST',
- 'content' => http_build_query( $post, '', ' &' )
- );
}
- return file_get_contents ( $url, false, stream_context_create ( $context ) );
- }
$data = array (
- 'name' => 'test',
- 'email' => 'admin@admin.com',
- 'submit' => 'submit',
- );
- echo Post ( 'http://bbs .it-home.org', $data );
-
Copy the code
That’s it. The above function is still good, and it also solves the problems of timeout control and Post value transfer.
|