Remote file localization with STREAM_ series functions, how to prevent card death

Source: Internet
Author: User
Tags sprintf
Remote file localization with STREAM_ series functions, how to prevent the card from dying
Or I wrote the first two days of code, has been used to collect content, today used to improve the acquisition of the content of the image and other remote file localization, sometimes will be stuck, master see how to prevent the card to die.
Code below, this code also provides two ways to achieve localization comparison, the current situation proves that this flow mode implementation efficiency 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 has data for us, or is 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], ' $ ')? $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--------------------
Since PHP does not have timers, you cannot interrupt the execution of a function that does not interrupt the interface.
So the underlying function is only suitable for the ideal condition

I still suggest you use multiple curl to do your job.
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); In this loop, you have the opportunity to interrupt the execution of the program.  Curl_getinfo provides various information about the connection}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.