Table of Contents
php curl setting timeout example, phpcurl example
curl timeout output
Home Backend Development PHP Tutorial php curl setting timeout example, phpcurl example_PHP tutorial

php curl setting timeout example, phpcurl example_PHP tutorial

Jul 13, 2016 am 10:15 AM
curl php time out

php curl setting timeout example, phpcurl example

The example in this article describes the curl timeout setting method in PHP. Share it with everyone for your reference. The specific implementation method is as follows:

There are many ways to access HTTP, you can use curl, socket, file_get_contents() and other methods.
When accessing http, you need to consider the issue of timeout.

CURL access HTTP:

CURL is a commonly used lib library for accessing HTTP protocol interfaces. It has high performance and has some concurrency support functions.
curl_setopt($ch, opt) can set some timeout settings, mainly including:
① (Important) CURLOPT_TIMEOUT sets the maximum number of seconds that cURL is allowed to execute.       
② (Important) CURLOPT_TIMEOUT_MS sets the maximum number of milliseconds that cURL is allowed to execute. 
(Added in cURL 7.16.2. Available from PHP 5.2.3 onwards)
③ CURLOPT_CONNECTTIMEOUT is the time to wait before initiating a connection. If set to 0, it will wait indefinitely.
④ CURLOPT_CONNECTTIMEOUT_MS The time to wait for trying to connect, in milliseconds. If set to 0, wait infinitely. (Added in cURL 7.16.2. Available since PHP 5.2.3)
⑤ CURLOPT_DNS_CACHE_TIMEOUT sets the time to save DNS information in memory, the default is 120 seconds.

1. curl normal second-level timeout:

Copy code The code is as follows:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT,60); //Just set the number of seconds
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);


2. Curl normal second-level timeout use:


Copy code

The code is as follows: curl_setopt($ch, CURLOPT_TIMEOUT,60);
3. If curl requires millisecond timeout, you need to increase:


Copy code

The code is as follows: curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L); //or  curl_setopt ($ch, CURLOPT_NOSIGNAL,true);//Support millisecond level timeout setting

I hope this article will be helpful to everyone’s PHP programming.


Several common PHP timeout handling methods

[Web server timeout processing]

[ Apache ]
Generally, when the performance is very high, all default timeout configurations are 30 seconds, but when uploading files, or the network speed is very slow In this case, a timeout operation may be triggered.
There are currently three timeout settings in apachefastcgiphp-fpm mode:
fastcgi timeout setting:
Modified fastcgi connection configuration, similar to the following:

Copy the code as follows:

<IfModulemod_fastcgi.c>
FastCgiExternalServer/home/forum/apache/apache_php/cgi-bin/php-cgi-socket/home/forum/php5/etc/php-fpm.sock
ScriptAlias/fcgi-bin /"/home/forum/apache/apache_php/cgi-bin/"
AddHandlerphp-fastcgi.php
Actionphp-fastcgi/fcgi-bin/php-cgi
AddTypeapplication/x-
< /IfModule>

The default configuration is 30s. If you need to customize your own configuration, you need to modify the configuration, for example, to 100 seconds: (Restart apache after modification):

Copy the code as follows:

 <IfModulemod_fastcgi.c>
FastCgiExternalServer/home/forum/apache/apache_php/cgi-bin/php-cgi-socket/home/forum/php5/etc/php-fpm.sock-idle -timeout100
ScriptAlias/fcgi-bin/"/home/forum/apache/apache_php/cgi-bin/"
AddHandlerphp-fastcgi.php
Actionphp-fastcgi/fcgi-bin/php-cgi
AddTypeapplication/x-
</IfModule>

If it times out, it will return a 500 error, disconnect from the backend php service, and record an apache error log:
[ThuJan2718:30 :152011][error][client10.81.41.110]FastCGI:commwithserver"/home/forum/apache/apache_php/cgi-bin/php-cgi"aborted:idletimeout(30sec)
[ThuJan2718:30:152011] [error][client10.81.41.110]FastCGI:incompleteheaders(0bytes)receivedfromserver"/home/forum/apache/apache_php/cgi-bin/php-cgi"
Other fastcgi...the rest of the full text>>

curl timeout output

function curl_file_get_contents($url){
$starttime = time();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = trim(curl_exec($ch)) ;
curl_close($ch);

$endtime = time();

if($endtime-$starttime>=15){
echo 'php running timeout' ;
}

return $contents;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904928.htmlTechArticlephp curl setting timeout example, phpcurl example This article describes the curl timeout setting method in php. Share it with everyone for your reference. The specific implementation method is as follows: There are many ways to access HTTP,...
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 Article Tags

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles