Why does fsockopen fail?

Source: Internet
Author: User
What are the causes of fwrite failure when fsockopen is successful? Tested locally and on a WINDOWS Server. However, on a LINUX server, fsockopen is successful every time, but fwrite fails for a long time .. Who has encountered such a situation .. In addition, the // annotation mark that part, which can be explained .. The code is as follows: PHPcode $ match fsockopen is successful. What are the causes of fwrite failure?

Tested locally and on a WINDOWS Server.

However, on a LINUX server, fsockopen is successful every time, but fwrite fails for a long time ..

Who has encountered such a situation ..
In addition, the // annotation mark that part, which can be explained ..

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"; $ o Ut. = "Cookie: $ cookie \ 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";} $ 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); // annotation 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 ;}}} * // annotation Mark @ fclose ($ fp); return true ;}





------ Solution --------------------
Check the file read/write.
------ Solution --------------------
PHP code
/* For the main application, some TCP socket information is obtained here */$ status = stream_get_meta_data ($ fp ); /* check whether the TCP connection times out */if (! $ Status ['timed _ out']) {/* if no timeout occurs, read the data until the file terminator */while (! Feof ($ fp) {/* because the HTTP header information is read here, the empty line header information ends, therefore, you need break */if ($ header = @ fgets ($ fp )) & ($ header = "\ r \ n" | $ header = "\ n") {break ;}/ * read the HTTP body, I personally think the processing here is a bit rough. at least first check whether the Content-Length in the HTTP response header is processed according to it. if it is not read as follows */$ stop = false; while (! Feof ($ fp )&&! $ Stop) {/* read a maximum of 8192 bytes ($ limit decreasing) */$ data = fread ($ fp, ($ limit = 0 | $ limit> 8192? 8192: $ limit);/* connect the read content to the returned string */$ return. = $ data; if ($ limit) {$ limit-= strlen ($ data); $ stop = $ limit <= 0 ;}}}
------ Solution --------------------

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.