How to post data from one php file to another without using forms and hidden variables
Source: Internet
Author: User
You can use the following functions: functionposttohost ($ url, $ data) {$ urlparse_url ($ url); if (! $ Url) returncouldntparseurl; if (! Isset ($ url [port]) {$ url [port];} if (! Isset ($ url [query]) {$ url [query] ;}$ encoded; while (list ($ k, $ v) can be implemented using the following functions:
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 );
}
$ Fp = fsockopen ($ url ['host'], $ url ['port']? $ Url ['port']: 80 );
If (! $ Fp) return "Failed to open socket to $ url [host]";
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 );
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.