Sample code for using a socket in PHP to impersonate a post or get submit data in HTTP _php tutorial

Source: Internet
Author: User
Don't say much nonsense. Directly on the code:
sock_post.php:
Copy CodeThe code is as follows:
function Sock_post ($url, $data = ") {
$url = Parse_url ($url);
$url [' scheme '] | | $url [' scheme '] = ' http ';
$url [' Host '] | | $url [' host '] = $_server[' http_host '];
$url [' path '][0]! = '/' && $url [' path '] = '/'. $url [' Path '];

$query = $data;
if (Is_array ($data)) $query = Http_build_query ($data);

$fp = @fsockopen ($url [' Host '], $url [' Port ']? $url [' Port ']: 80);
if (! $fp) return "Failed to open socket to $url [host]";

Fputs ($FP, sprintf ("POST%s%s%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 ($query). "/n");
Fputs ($fp, "connection:close/n/n");

Fputs ($FP, "$query/n");

$line = fgets ($fp, 1024);

if (@!eregi ("^http/1/. ", $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;
}
$re = Sock_post (' http://localhost/direct_post/get_post.php?id=1 ', Array (' wel ' = ' hello '));
Echo $re;
?>

get_post.php:
Copy the Code code as follows:
echo "Post:";
Print_r ($_post);
Echo ' Get: ';
Print_r ($_get);
?>

http://www.bkjia.com/PHPjc/328127.html www.bkjia.com true http://www.bkjia.com/PHPjc/328127.html techarticle don't say much nonsense. Directly on the code: sock_post.php: Copy code code as follows: Php function Sock_post ($url, $data = ") {$url = Parse_url ($url); $url [' scheme '] | | $url [ ' Scheme ' = ...

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