_php tips for submitting data to a specific page using PHP Socke

Source: Internet
Author: User
With the load post in jquery and so on, I can't get the results I want! So suddenly-----all the things I think about these days, and now I have to.
Now you want to submit the data to the specified page in PHP, you should have it, and you can have a return value
So I found this function, but did not try, I think I can not know whether the space can pass through
It was brought by the village!
Copy Code code as follows:

<?php
/*-----------------------------------------------------------
* Function: Use PHP Socke to submit data to the specified page
*
* Author: honghe.c
*
* Description: Post ($url, $data)
*
* $url = ' http://www.jb51.net ';
* $data [user] = ' ";
* $data [Pass] = ' Xowldo ';
* Echo Post ($url, $data);
*-----------------------------------------------------------*/

Function post ($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 (the 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 the open socket to $url [host]";

Fputs ($FP, sprintf ("POST%s%s%s http/1.0n", $url [' Path '], $url [' query ']? "?": "", $url [' query ']]);
Fputs ($FP, "Host: $url [host]n");
Fputs ($fp, "Content-type:application/x-www-form-urlencodedn");
Fputs ($FP, "Content-length:". Strlen ($encoded). "N");
Fputs ($fp, "Connection:closenn");

Fputs ($fp, "$encodedn");

$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 = = "RN")) {
$inheader = 0;
}
ElseIf (! $inheader) {
$results. = $line;
}
}
Fclose ($FP);

return $results;
}

/*
$url = ' http://video.xxx.com:80/game_vm.php ';
$data [' gid '] = ' 1 ';
Echo Post ($url, $data);
*/

?>

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.