How to prevent remote file localization using stream _ series functions

Source: Internet
Author: User
The stream _ series functions are used to implement remote file localization. the code I wrote two days ago on how to prevent freezing is already used to collect content, today, it is used to improve the localization of images and other remote files in the collected content. sometimes it will still get stuck. The code is as follows. this code provides two methods for local comparison. Current Affairs prove that the efficiency of this stream method is improved. PHPcode & lt ;? Php $ dirstr_replace uses stream _ series functions to implement remote file localization. how can this problem be prevented from getting stuck?
The code I wrote two days ago has already been used to collect content. Today, the code is used to improve the localization of images and other remote files in the collected content. sometimes it will still get stuck. experts can see how to prevent it from getting stuck.
The code is as follows. this code provides two methods for local comparison. Current Affairs prove that the efficiency of this stream method is improved.
PHP code
  
  ';$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);?>




------ Solution --------------------
Because php does not have a timer, you cannot interrupt the execution of a function without an interrupt interface.
Therefore, the underlying functions are only suitable for ideal conditions.

I still recommend that you use multiple curls to complete your work.
PHP code
$ Urls = array ('http: // response, 'http: // response, '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_mu Lti_exec ($ mh, $ active); // in this loop, you have the opportunity to interrupt program execution. Curl_getinfo provides various connection information} while ($ active); foreach ($ urls as $ I =>$ url) {$ fn = basename ($ url ); file_put_contents ($ fn, curl_multi_getcontent ($ conn [$ I]); curl_close ($ conn [$ I]);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.