PHP POST Request garbled problem

Source: Internet
Author: User
PHP garbled

Use the most Earth template in the POST request source code as follows
static public Function DoPost ($url, $post _data=array ()) {$url 2 = Parse_url ($url); $url 2["path"] = ($url 2["path"] = = ""? " /": $url 2[" path "]); $url 2[" port "] = ($url 2[" port "] = =" "? $url 2["Port"]), $host _ip = @gethostbyname ($url 2["host"); $fsock _timeout = 2; 2 Secondif ($fsock = Fsockopen ($host _ip, $url 2[' Port '], $errno, $errstr, $fsock _timeout)) < 0) {return false;} $request = $url 2["path"]. ($url 2["Query"]? "?" . $url 2["Query"]: $post _data2 = http_build_query ($post _data); $post _data2=urldecode ($post _data2); $in = "POST". $request. "http/1.0\r\n"; $in. = "Accept: */*\r\n"; $in. = "Host:". $url 2["Host"]. "\ r \ n"; $in. = "user-agent:lowell-agent\r\n"; $in. = "content-type:application/x-www-form-urlencoded\r\n"; $in. = " Content-length: ". strlen ($post _data2). "\ r \ n"; $in. = "connection:close\r\n\r\n"; $in. = $post _data2. "\r\n\r\n"; unset ($post _data2); if (! @fwrite ($fsock, $in, strlen ($in))) {fclose ($fsock); return false;} Return self::gethttpcontent ($fsock);} Static Private FunctiOn Gethttpcontent ($fsock =null) {$out = Null;while ($buff = @fgets ($fsock, 2048)) {$out. = $buff;}    Fclose ($fsock); $pos = Strpos ($out, "\r\n\r\n"); $head = substr ($out, 0, $pos);    HTTP Head$status = substr ($head, 0, Strpos ($head, "\ r \ n")); HTTP Status line$body = substr ($out, $pos + 4, strlen ($out)-($pos + 4)),//page Bodyif (Preg_match ("/^http\/\d\.\d\s" ([\d  ]+) \s.*$/", $status, $matches)) {if (Intval ($matches [1])/+ = 2) {return $body; }else{return false;}} Else{return false;}}

The implementation of the HTTP text message is sent because the operator did not urldecode I can only think of the parameters of the original text request sent to the phone text message is garbled I contact PHP also soon want to ask you have any way to solve garbled problem.
It is not possible to request HTTP by other means.


Reply to discussion (solution)

Garbled may be caused by inconsistent coding on both sides, such as the operator there is GBK code, your page with the UTF8 code, you put your page and the operator to keep consistent try.

Garbled may be caused by inconsistent coding on both sides, such as the operator there is GBK code, your page with the UTF8 code, you put your page and the operator to keep consistent try.
Tried it or garbled it.


Somebody, man.

First ask the recipient's coding requirements and then encode as needed.

First ask the recipient's coding requirements and then encode as needed.
They say the code is GB2312 I set the page charset settings gb2312 send results or garbled.


First ask the recipient's coding requirements and then encode as needed.
They say the code is GB2312 I set the page charset settings gb2312 send results or garbled.

Send the character to do a iconv

I'm looking at your head, it's empty.

The key is you send the past data is GBK, not change CharSet on the line

I'm looking at your head, it's empty.

The key is you send the past data is GBK, not change CharSet on the line
Haha, be attentive.
What does the above code do to deal with the GBK?

It's not about the code, it's going to be converted before it's passed

It's not about the code, it's going to be converted before it's passed
Thank you for your guidance. It worked.

The twist of the good cow B

  • 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.