Php code about http Request

Source: Internet
Author: User
Php code, read for a long time, still do not understand. Who can translate it into c #'s publicfunctionget_userinfo ($ uid) {$ paramsarray (); $ params [uid] $ uid; return $ this-post ($ params, api. tuan.2345.comapigetUserInfo. php);} privatefunctionpost ($ data, $ url

Php code, read for a long time, still do not understand. Who can translate it into public function get_userinfo ($ uid) {$ params = array (); $ params ['uid'] = $ uid; return $ this-post ($ params, "http://api.tuan.2345.com/api/getUserInfo.php");} private function post ($ data, $ url

Php code, read for a long time, still do not understand. Who can translate it into c #

Public function get_userinfo ($ uid ){
$ Params = array ();
$ Params ['uid'] = $ uid;
Return $ this-> post ($ params, "http://api.tuan.2345.com/api/getUserInfo.php ");

}
Private function post ($ data, $ url)
{/*{{{*/
// Get parts of URL
$ Url = parse_url ($ url );
If (! $ Url) {return "couldn't parse url ";}

// Provide defaults for port and query string
If (! Isset ($ url ['Port']) {$ url ['Port'] = "";}
If (! Isset ($ url ['query']) {$ url ['query'] = "";}

// Build POST string
$ Encoded = "";
Foreach ($ data as $ k => $ v ){
$ Encoded. = ($ encoded? "&":"");
$ Encoded. = rawurlencode ($ k). "=". rawurlencode ($ v );
}
// Open socket on host
// $ Encoded/* string */= uid = % A1 % B0608496 % A1 % B1
$ Fp = @ fsockopen ($ url ['host'], $ url ['Port']? $ Url ['Port']: 80 );
If (! $ Fp) {return "failed to open socket to {$ url ['host']}";}

// Send HTTP 1.0 POST request to 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 ");

// Read the first line of data, only accept if 200 OK is sent
$ Line = fgets ($ fp, 1024 );

If (! Preg_match ('# ^ HTTP/1 \\.. 200 #', $ line) {return ;}

// Put everything, cannot the headers to $ results
$ Results = "";
$ Inheader = TRUE;
While (! Feof ($ fp )){
$ Line = fgets ($ fp, 1024 );

If ($ inheader & ($ line = "\ n" | $ line = "\ r \ n ")){
$ Inheader = FALSE;
}
Elseif (! $ Inheader ){
$ Results. = $ line;
}
}

Fclose ($ fp );

// Return with data encoded ed
Return $ results;
}/*}}}*/

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.