Use PHPsocke to submit data to the specified page _ PHP Tutorial

Source: Internet
Author: User
Use PHPsocke to submit data to the specified page. Using loadpost in jquery and so on cannot get the desired result! So suddenly, I thought about all the things I thought about in the past few days. now I have to do this. now I want to submit data to the specified page in php and use load post in jquery, unable to get the desired result! So suddenly ----- I thought about things in the past few days, so now I have.
If you want to submit data to the specified page in php, there should be and return values.
So I found this function, but I didn't try it. I don't know if the space can be used.
It's from the village!

The code is as follows:


/*-----------------------------------------------------------
* Function: use PHP socke to submit data to a specified page.
*
* Author: Honghe. c
*
* Description: post ($ url, $ data)
*
* $ Url = 'http: // www.jb51.net ';
* $ Data [user] = 'Hong ';
* $ Data [pass] = 'xowld ';
* 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 (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 open socket to $ url [host]";

Fputs ($ fp, sprintf ("POST % 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 .. 200", $ 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 );
*/

?>

Success! So suddenly ----- I thought about things in the past few days. now I have to do this. now I want to submit data to the specified page in php...

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.