A php function for reading remote files
A function for reading remote files, which is very useful!
- Function urlfopen ($ url, $ limit = 0, $ post = '', $ cookie ='', $ bysocket = FALSE, $ ip = '', $ timeout = 15, $ block = TRUE, $ encodetype = 'urlencode '){
- $ Return = '';
- $ Matches = parse_url ($ url );
- $ Host = $ matches ['host'];
- $ Path = $ matches ['path']? $ Matches ['path']. (isset ($ 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 ";
- $ Boundary = $ encodetype = 'urlencode '? '': ';'. Substr ($ post, 0, trim (strpos ($ post," \ n ")));
- $ Out. = $ encodetype = 'urlencode '? "Content-Type: application/x-www-form-urlencoded \ r \ n": "Content-Type: multipart/form-data $ boundary \ 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 ";
- $ 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. = "Referer: \ r \ n ";
- $ Out. = "Connection: Close \ r \ n ";
- $ Out. = "Cookie: $ cookie \ r \ n ";
- }
- $ Fp = @ fsockopen ($ ip? $ Ip: $ host), $ port, $ errno, $ errstr, $ timeout );
- If (! $ Fp ){
- Return '';
- } Else {
- Stream_set_blocking ($ fp, $ block );
- Stream_set_timeout ($ fp, $ timeout );
- @ Fwrite ($ fp, $ out );
- $ 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;
- }
- }
- }
- @ Fclose ($ fp );
- Return $ return;
- }
- }
|
PHP