Home Backend Development PHP Tutorial 用stream_系列函数实现远程文件本地化,怎么防止卡死

用stream_系列函数实现远程文件本地化,怎么防止卡死

Jun 13, 2016 pm 01:10 PM
http quot sockets tmp

用stream_系列函数实现远程文件本地化,如何防止卡死
还是我前两天写的代码,已经用于采集内容了,今天用来改进采集到的内容中的图片等远程文件的本地化,有时候还是会卡死,高手看看如何能防止卡死。
代码如下,这个代码还是提供两种方式实现本地化进行比较,时事证明这种流方式实现效率是提高了。

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php $dir = str_replace('\\', '/', dirname(__FILE__)) . '/';
$timeStart = microtime(true);
$data = '';
$urls = array('http://www.jxcms.com/upload/2011/0518/0337319304.jpg', 'http://www.jxcms.com/upload/2011/0310/0951568835.jpg', 'http://www.tzksgs.com/upload/banner1.jpg', 'http://www.tzksgs.com/upload/banner2.jpg');
foreach($urls as $url) {
    copy($url, $dir . 'a_' . basename($url));
}
$timeEnd = microtime(true);
echo sprintf("Spend time: %s second(s)\n", $timeEnd - $timeStart), '<br>';
$timeStart = microtime(true);
function getMoreContent($urls) {
    $timeout = 30;
    $rs = array();
    $sockets = array();
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    foreach($urls as $id => $url) {
        $tmp = parse_url($url);
        $host = $tmp['host'];
        $path = isset($tmp['path'])?$tmp['path']:'/';
        empty($tmp['query']) or $path .= '?' . $tmp['query'];
        if (empty($tmp['port'])) {
            $port = $tmp['scheme'] == 'https'?443:80;
        } else $port = $tmp['port'];
        $fp = stream_socket_client("$host:$port", $errno, $errstr, $timeout);
        if ($fp) {
            $rs[$id] = '';
            $sockets[$id] = $fp;
            fwrite($fp, "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: $userAgent\r\nConnection: Close\r\n\r\n");
        }
    }
    // Now, wait for the results to come back in
    while (count($sockets)) {
        $read = $sockets;
        // This is the magic function - explained below
        if (stream_select($read, $write = null, $e = null, $timeout)) {
            // readable sockets either have data for us, or are failed connection attempts
            foreach ($read as $r) {
                $id = array_search($r, $sockets);
                $data = fread($r, 8192);
                if (strlen($data) == 0) {
                    fclose($r);
                    $tmp = explode("\r\n\r\n", $rs[$id], 2);
                    $rs[$id] = strpos($tmp[0], '200')?$tmp[1]:'';
                    unset($sockets[$id]);
                } else $rs[$id] .= $data;
            }
        }
    }
    return $rs;
}
$rs = getMoreContent($urls);
foreach($rs as $k => $v) {
    @file_put_contents($dir . 'b_' . basename($urls[$k]), $v);
}
$timeEnd = microtime(true);
echo sprintf("Spend time: %s second(s)\n", $timeEnd - $timeStart);

?>

Copy after login




------解决方案--------------------
由于php没有计时器,所以你不可能中断一个没有中断接口的函数的执行
所以底层函数都只适合于理想的条件下

我依然建议你使用多道的curl来完成你的工作
PHP code
$urls = array(
 'http://www.jxcms.com/upload/2011/0518/0337319304.jpg',
 'http://www.jxcms.com/upload/2011/0310/0951568835.jpg',
 'http://www.tzksgs.com/upload/banner1.jpg',
 'http://www.tzksgs.com/upload/banner2.jpg'
);
$mh = curl_multi_init();

foreach ($urls as $i => $url) {
       $conn[$i] = curl_init($url);
       curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER,1);
       curl_multi_add_handle($mh, $conn[$i]);
}

do {
  curl_multi_exec($mh, $active);
  //在这个循环中,你有机会中断程序的执行。curl_getinfo提供了连接的各种信息
}while($active);

foreach ($urls as $i => $url) {
  $fn = basename($url);
  file_put_contents($fn, curl_multi_getcontent($conn[$i]));
  curl_close($conn[$i]);
} <div class="clear">
                 
              
              
        
            </div>
Copy after login
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

Video Face Swap

Video Face Swap

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

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
The cleaning principle of /tmp/ folder in Linux system and the role of tmp file The cleaning principle of /tmp/ folder in Linux system and the role of tmp file Dec 21, 2023 pm 05:36 PM

Most of the .tmp files are files left behind due to abnormal shutdown or crash. These temporary scratch disks have no use after you restart the computer, so you can safely delete them. When you use the Windows operating system, you may often find some files with the suffix TMP in the root directory of the C drive, and you will also find a TEMP directory in the Windows directory. TMP files are temporary files generated by various software or systems. , also known as junk files. Temporary files generated by Windows are essentially the same as virtual memory, except that temporary files are more targeted than virtual memory and only serve a certain program. And its specificity has caused many novices to be intimidated by it and not delete it.

What does http status code 520 mean? What does http status code 520 mean? Oct 13, 2023 pm 03:11 PM

HTTP status code 520 means that the server encountered an unknown error while processing the request and cannot provide more specific information. Used to indicate that an unknown error occurred when the server was processing the request, which may be caused by server configuration problems, network problems, or other unknown reasons. This is usually caused by server configuration issues, network issues, server overload, or coding errors. If you encounter a status code 520 error, it is best to contact the website administrator or technical support team for more information and assistance.

How to access and clean junk files in /tmp directory in CentOS 7? How to access and clean junk files in /tmp directory in CentOS 7? Dec 27, 2023 pm 09:10 PM

There is a lot of garbage in the tmp directory in the centos7 system. If you want to clear the garbage, how should you do it? Let’s take a look at the detailed tutorial below. To view the list of files in the tmp file directory, execute the command cdtmp/ to switch to the current file directory of tmp, and execute the ll command to view the list of files in the current directory. As shown below. Use the rm command to delete files. It should be noted that the rm command deletes files from the system forever. Therefore, it is recommended that when using the rm command, it is best to give a prompt before deleting the file. Use the command rm-i file name, wait for the user to confirm deletion (y) or skip deletion (n), and the system will perform corresponding operations. As shown below.

What is http status code 403? What is http status code 403? Oct 07, 2023 pm 02:04 PM

HTTP status code 403 means that the server rejected the client's request. The solution to http status code 403 is: 1. Check the authentication credentials. If the server requires authentication, ensure that the correct credentials are provided; 2. Check the IP address restrictions. If the server has restricted the IP address, ensure that the client's IP address is restricted. Whitelisted or not blacklisted; 3. Check the file permission settings. If the 403 status code is related to the permission settings of the file or directory, ensure that the client has sufficient permissions to access these files or directories, etc.

Understand common application scenarios of web page redirection and understand the HTTP 301 status code Understand common application scenarios of web page redirection and understand the HTTP 301 status code Feb 18, 2024 pm 08:41 PM

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

HTTP 200 OK: Understand the meaning and purpose of a successful response HTTP 200 OK: Understand the meaning and purpose of a successful response Dec 26, 2023 am 10:25 AM

HTTP Status Code 200: Explore the Meaning and Purpose of Successful Responses HTTP status codes are numeric codes used to indicate the status of a server's response. Among them, status code 200 indicates that the request has been successfully processed by the server. This article will explore the specific meaning and use of HTTP status code 200. First, let us understand the classification of HTTP status codes. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. Among them, 2xx indicates a successful response. And 200 is the most common status code in 2xx

How to use Nginx Proxy Manager to implement automatic jump from HTTP to HTTPS How to use Nginx Proxy Manager to implement automatic jump from HTTP to HTTPS Sep 26, 2023 am 11:19 AM

How to use NginxProxyManager to implement automatic jump from HTTP to HTTPS. With the development of the Internet, more and more websites are beginning to use the HTTPS protocol to encrypt data transmission to improve data security and user privacy protection. Since the HTTPS protocol requires the support of an SSL certificate, certain technical support is required when deploying the HTTPS protocol. Nginx is a powerful and commonly used HTTP server and reverse proxy server, and NginxProxy

Send POST request with form data using http.PostForm function Send POST request with form data using http.PostForm function Jul 25, 2023 pm 10:51 PM

Use the http.PostForm function to send a POST request with form data. In the http package of the Go language, you can use the http.PostForm function to send a POST request with form data. The prototype of the http.PostForm function is as follows: funcPostForm(urlstring,dataurl.Values)(resp*http.Response,errerror)where, u

See all articles