search
HomeBackend DevelopmentPHP ProblemHow to solve PHP request interface timeout

How to solve PHP request interface timeout

Sep 29, 2019 pm 05:48 PM
phpinterfacetime out

How to solve PHP request interface timeout

[HTTP access]

Generally we access HTTP in many ways, mainly: curl, socket, file_get_contents() and other methods.

If the other party's server never responds, we will be in tragedy. It is easy to kill the entire server, so we also need to consider the timeout issue when accessing http.

[CURL access HTTP]

CURL is a commonly used and reliable lib library for accessing the HTTP protocol interface. It has high performance and some concurrency support. functions, etc.

CURL:

curl_setopt($ch, opt) can set some timeout settings, mainly including:

CURLOPT_TIMEOUT Set the maximum number of seconds cURL is allowed to execute.

CURLOPT_TIMEOUT_MS Sets the maximum number of milliseconds cURL is allowed to execute. (Added in cURL 7.16.2. Available from PHP 5.2.3.)

CURLOPT_CONNECTTIMEOUT The time to wait before initiating a connection. If set to 0, it will wait indefinitely.

CURLOPT_CONNECTTIMEOUT_MS The time to wait for a connection attempt, in milliseconds. If set to 0, wait infinitely. Added in cURL 7.16.2. Available starting with PHP 5.2.3.

CURLOPT_DNS_CACHE_TIMEOUT Set the time to save DNS information in memory, the default is 120 seconds.

curl ordinary second-level timeout:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_TIMEOUT, 60); //You only need to set the number of seconds

curl_setopt($ ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);

curl ordinary second-level timeout usage:

curl_setopt($ch, CURLOPT_TIMEOUT, 60);

If curl requires millisecond timeout, you need to add:

curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L );

Or:

curl_setopt ($ch, CURLOPT_NOSIGNAL, true); can support millisecond level timeout settings

An example of a millisecond timeout for curl:

<?php
if (!isset($_GET[&#39;foo&#39;])) {
    
// Client
    $ch = curl_init(&#39;http://example.com/&#39;);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);  
//注意,毫秒超时一定要设置这个
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); 
//超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用
    $data = curl_exec($ch);
    $curl_errno = curl_errno($ch);
    $curl_error = curl_error($ch);
    curl_close($ch);
 
    if ($curl_errno > 0) {
        echo "cURL Error ($curl_errno): $curl_error\n";
    } else {
        echo "Data received: $data\n";
    }
} else {
    
// Server
    sleep(10);
    echo "Done.";
}
?>

Some other tips:

1. According to experience, the summary is: cURL version >= libcurl/7.21. In version 0, the millisecond timeout will definitely take effect, remember.

2. There is also a problem with the millisecond timeout of curl_multi. . A single access supports ms-level timeout, but curl_multi will be inaccurate if multiple calls are made in parallel.

[Accessing HTTP via stream processing]

In addition to curl, we often also Use fsockopen or file operation function to process the HTTP protocol, so our timeout processing for this is also necessary.

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to solve PHP request interface timeout. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.