Home Backend Development PHP Tutorial file_get_contents Several solutions to the timeout of the PHP file_get_contents function

file_get_contents Several solutions to the timeout of the PHP file_get_contents function

Jul 29, 2016 am 08:40 AM
file_get_contents

Here are two brief introductions:
1. Increase the time limit of timeout
Note here: set_time_limit only sets the timeout of your PHP program, not the timeout of the file_get_contents function reading the URL.
I initially thought that set_time_limit could also affect file_get_contents, but after testing, it was invalid. To truly modify the file_get_contents delay, you can use the timeout parameter of resource $context:

Copy the code The code is as follows:


$opts = array(
'http'=>array(
'method'=> "GET",
'timeout'=>60,
)
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.example.com', false, $context ; If it still fails, give up, because file_get_contents() will return FALSE if it fails, so you can write the code as follows:


Copy the code

The code is as follows:

$cnt=0; while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE) $cnt++;

The above introduces several solutions to the file_get_contents PHP file_get_contents function timeout, including the content of file_get_contents. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve PHP Warning: file_get_contents(): Filename cannot be empty How to solve PHP Warning: file_get_contents(): Filename cannot be empty Aug 18, 2023 pm 07:30 PM

How to solve PHPWarning: file_get_contents(): Filenamecannotbeempty In the process of PHP development, we often encounter this error message: PHPWarning: file_get_contents(): Filenamecannotbeempty. This error usually occurs when using the file_get_contents function

如何解决PHP Warning: file_get_contents(): failed to open stream: HTTP request failed 如何解决PHP Warning: file_get_contents(): failed to open stream: HTTP request failed Aug 18, 2023 pm 11:34 PM

How to solve PHPWarning:file_get_contents():failedtoopenstream:HTTPrequestfailed During PHP development, we often encounter situations where HTTP requests are initiated to remote servers through the file_get_contents function. However, sometimes we encounter a common error message: PHPWarning: file_get_c

PHP's file_get_contents() function: How to read contents from a file PHP's file_get_contents() function: How to read contents from a file Nov 04, 2023 pm 01:43 PM

PHP's file_get_contents() function: How to read content from a file, specific code example In PHP, file_get_contents() is a very useful function that allows us to read content from a file. Whether reading a text file or reading content from a remote URL, this function can easily complete the task. Syntax The basic syntax of this function is as follows: stringfile_get_contents(string$f

Detailed explanation of PHP file caching functions: file caching processing methods of file_get_contents, file_put_contents, unlink and other functions Detailed explanation of PHP file caching functions: file caching processing methods of file_get_contents, file_put_contents, unlink and other functions Nov 18, 2023 am 09:37 AM

Detailed explanation of PHP file caching functions: file caching processing methods of file_get_contents, file_put_contents, unlink and other functions, which require specific code examples. In web development, we often need to read data from files or write data to files. Moreover, in some cases, we need to cache the contents of files to avoid frequent file read and write operations, thus improving performance. In PHP, there are several commonly used functions that can help us implement file caching, including

PHP function introduction—file_get_contents(): Read the contents of the URL into a string PHP function introduction—file_get_contents(): Read the contents of the URL into a string Jul 24, 2023 pm 02:32 PM

PHP function introduction—file_get_contents(): Read the contents of the URL into a string. In web development, it is often necessary to obtain data from a remote server or read a remote file. PHP provides a very powerful function file_get_contents(), which can conveniently read the contents of a URL and save it to a string. This article will introduce the usage of file_get_contents() function and give some code examples to help readers better

How to read file contents using file_get_contents function in PHP How to read file contents using file_get_contents function in PHP Jun 26, 2023 pm 12:01 PM

In PHP, we often need to read data from files. In this case, we can use the file_get_contents function. This function can simply read everything from a file and return it as a string. This is very useful in many scenarios, such as reading configuration files, reading log files, etc. In this article, we will explain how to read file contents using the file_get_contents function in PHP. Step 1: Open the file using file

Detailed explanation of PHP 5.2 functions: How to use the file_get_contents function to read file contents Detailed explanation of PHP 5.2 functions: How to use the file_get_contents function to read file contents Jul 29, 2023 pm 04:09 PM

Detailed explanation of PHP5.2 functions: How to use the file_get_contents function to read file contents. In PHP development, we often need to read the contents of files. PHP provides many methods to read file contents. One of the commonly used and powerful functions is file_get_contents(). This function can read the content from a file and return the content in the form of a string to facilitate our subsequent processing. file_get_contents() function

PHP function introduction—file_get_contents(): Read file contents into a string PHP function introduction—file_get_contents(): Read file contents into a string Jul 24, 2023 pm 06:40 PM

Introduction to PHP functions—file_get_contents(): Read file contents into strings. In PHP development, it is often necessary to read the contents of files and process them. To achieve this function, you can use PHP's built-in function file_get_contents(). This article will introduce the file_get_contents() function and provide some code examples to help readers better understand its usage. Basic introduction to file_get_contents function fi

See all articles