POST messages to the host in php, and post requests using code POST messages to the host in php, and post requests using code
/* ** POST messages to the host */ Function PostToHost ($ url, $ data ){ $ Url = parse_url ($ url ); If (! $ Url) return "couldn \'t parse url "; If (! Isset ($ url [\ 'Port \ ']) {$ url [\ 'Port \'] = "";} If (! Isset ($ url [\ 'query \ ']) {$ url [\ 'query \'] = "";}
$ Encoded = ""; While (list ($ k, $ v) = each ($ data )){ $ Encoded. = ($ encoded? "&":""); $ Encoded. = rawurlencode ($ k). "=". rawurlencode ($ v ); } $ Port = $ url [\ 'Port \ ']? $ Url [\ 'Port \ ']: 80; $ Fp = fsockopen ($ url [\ 'host \ '], $ port, $ errno, $ errstr ); If (! $ Fp) return "Failed to open socket to $ url [host] $ port ERROR: $ errno-$ errstr "; Fputs ($ fp, sprintf ("POST % s HTTP/1.0 \ n", $ url [\ 'path \ '], $ url [\ 'query \ ']? "? ":" ", $ Url [\ 'query \ ']); Fputs ($ fp, "Host: $ url [host] \ n "); Fputs ($ fp, "Content-type: application/x-www-form-urlencoded \ n "); Fputs ($ fp, "Content-length:". strlen ($ encoded). "\ n "); Fputs ($ fp, "Connection: close \ n "); Fputs ($ fp, "$ encoded \ n "); $ Line = fgets ($ fp, 1024 ); If (! Eregi ("^ HTTP/1 \ .. 200", $ line) return; $ Results = ""; $ inheader = 1; While (! Feof ($ fp )){ $ Line = fgets ($ fp, 1024 ); If ($ inheader & ($ line = "\ n" | $ line = "\ r \ n ")){ $ Inheader = 0; } Elseif (! $ Inheader ){ $ Results. = $ line; } } Fclose ($ fp ); Return $ results; }/* End function PostToHost */ ?>
|
POST messages to the host in php, and post requests using code
/* ** POST messages to the host */ Function PostToHost ($ url, $ data ){ $ Url = parse_url ($ url ); If (! $ Url) return "couldn \'t parse url "; If (! Isset ($ url [\ 'Port \ ']) {$ url [\ 'Port \'] = "";} If (! Isset ($ url [\ 'query \ ']) {$ url [\ 'query \'] = "";}
$ Encoded = ""; While (list ($ k, $ v) = each ($ data )){ $ Encoded. = ($ encoded? "&":""); $ Encoded. = rawurlencode ($ k). "=". rawurlencode ($ v ); } $ Port = $ url [\ 'Port \ ']? $ Url [\ 'Port \ ']: 80; $ Fp = fsockopen ($ url [\ 'host \ '], $ port, $ errno, $ errstr ); If (! $ Fp) return "Failed to open socket to $ url [host] $ port ERROR: $ errno-$ errstr "; Fputs ($ fp, sprintf ("POST % s HTTP/1.0 \ n", $ url [\ 'path \ '], $ url [\ 'query \ ']? "? ":" ", $ Url [\ 'query \ ']); Fputs ($ fp, "Host: $ url [host] \ n "); Fputs ($ fp, "Content-type: application/x-www-form-urlencoded \ n "); Fputs ($ fp, "Content-length:". strlen ($ encoded). "\ n "); Fputs ($ fp, "Connection: close \ n "); Fputs ($ fp, "$ encoded \ n "); $ Line = fgets ($ fp, 1024 ); If (! Eregi ("^ HTTP/1 \ .. 200", $ line) return; $ Results = ""; $ inheader = 1; While (! Feof ($ fp )){ $ Line = fgets ($ fp, 1024 ); If ($ inheader & ($ line = "\ n" | $ line = "\ r \ n ")){ $ Inheader = 0; } Elseif (! $ Inheader ){ $ Results. = $ line; } } Fclose ($ fp ); Return $ results; }/* End function PostToHost */ ?>
|