What are the reasons for Fsockopen success and fwrite failure?
Testing both locally and on a Windows server is stable.
But on the Linux server, Fsockopen every time is also successful, but fwrite most of the failure, a long time to succeed.
Has anyone ever met such a situation?
In addition//comment mark that part, who can explain next.
The code is as follows:
PHP Code
$matches = Parse_url ($url); !isset ($matches [' Host ']) && $matches [' host '] = '; !isset ($matches [' path ']) && $matches [' path '] = '; !isset ($matches [' query ']) && $matches [' query '] = '; !isset ($matches [' Port ']) && $matches [' port '] = '; $host = $matches [' Host ']; $path = $matches [' Path ']? $matches [' Path ']. ($matches [' query ']? '?'. $matches [' query ']: '): '/'; $port =!empty ($matches [' Port '])? $matches [' Port ']: 80; if ($post) {$out = "post $path http/1.0\r\n"; $out. = "Accept: */*\r\n"; $out. = "accept-language:zh-cn\r\n"; $out. = "content-type:application/x-www-form-urlencoded\r\n"; $out. = "User-agent: $_server[http_user_agent]\r\n"; $out. = "Host: $host \ r \ n"; $out. = ' content-length: '. strlen ($post). " \ r \ n "; $out. = "connection:close\r\n"; $out. = "cache-control:no-cache\r\n"; $out. = "Cookie: $cookie \r\n\r\n"; $out. = $post; } else {$out= "GET $path http/1.0\r\n"; $out. = "Accept: */*\r\n"; $out. = "accept-language:zh-cn\r\n"; $out. = "User-agent: $_server[http_user_agent]\r\n"; $out. = "Host: $host \ r \ n"; $out. = "connection:close\r\n"; $out. = "Cookie: $cookie \r\n\r\n"; } $fp = @fsockopen (($ip? $ip: $host), $port, $errno, $errstr, $timeout); if (! $fp) {return ';//note $errstr: $errno \ r \ n} else {stream_set_blocking ($fp, $block); Stream_set_timeout ($fp, $timeout); Var_dump ($FP); @fwrite ($fp, $out);//comment mark/* $status = Stream_get_meta_data ($FP); if (! $status [' timed_out ']) {while (!feof ($fp)) {if ($header = @fgets ($fp)) && ($header = = "\ r \ n" | | $header = = "\ n")) {break; }} $stop = false; while (!feof ($FP) &&! $stop) {$data = Fread ($fp, ($limit = = 0 | | $limit > 8192 8192: $limit)) ; $return. = $data; if ($limit) {$limit-= strlen ($data); $stop = $limit <= 0; }}}*///Comment tag @fclose ($fp); return true; }
------Solution--------------------
Check the read and write of the file.
------Solution--------------------
PHP Code
/* for the application of the landlord, here is some information on TCP sockets */$status = Stream_get_meta_data ($FP);/* Here is the check T Whether the CP connection timed out */if (! $status [' timed_out ']) {/* If there is no timeout, then the data is read until a file terminator is encountered */while (!feof ($fp)) {/* as this is read HT TP header information, blank line ID header information ends, so break */if ($header = @fgets ($fp)) && ($header = = "\ r \ n" | | $header = = "\ n")) {break; }}/* here is the body that reads HTTP, personally think the processing here is somewhat rough, at least first look at the HTTP response header there is no content-length based on it to handle, if not read the following */$stop = FA Lse while (!feof ($FP) &&! $stop) {/* read up to 8192 bytes ($limit decrement) */$data = Fread ($fp, ($limit = = 0 | | $limit & Gt 8192? 8192: $limit));/* Connect the contents of the read to the return string */$return. = $data; if ($limit) {$limit-= strlen ($data); $stop = $limit <= 0; }}}
------solution--------------------