Problems with garbled PHP post requests

Source: Internet
Author: User
PHP POST Request garbled problem
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 Second
if ($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 body
if (Preg_match ("/^http\/\d\.\d\s ([\d]+) \s.*$/", $status, $matches)) {
if (Intval ($matches [1])/100 = = 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. PHP garbled
  • 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.