PHP-based GET and POST data-PHP Tutorial

Source: Internet
Author: User
Use PHP to implement GET and POST data. 1. how can I GET the webpage content in PHP? PhpfunctionsocketGet ($ url, $ ret) {$ urlArrparse_url ($ url); $ host $ urlArr [host]; $ portisset ($ urlArr [port])? $ UrlArr [port]: 80; $ pa 1. GET webpage content using PHP
Function socketGet ($ url, & $ ret)
{
$ UrlArr = parse_url ($ url );
$ Host = $ urlArr ['host'];
$ Port = isset ($ urlArr ['port'])? $ UrlArr ['port']: 80;
$ Path = isset ($ urlArr ['path'])? $ UrlArr ['path']: "/";
$ Fp = fsockopen ($ host, $ port, $ errno, $ errstr, 30 );
If (! $ Fp)
{
Echo "$ errstr ($ errno)
\ N ";
Return false;
}
Else
{
$ Out = "GET $ path HTTP/1.1 \ r \ n ";
$ Out. = "Host: $ host \ r \ n ";
$ Out. = "Connection: Close \ r \ n ";
$ Ret = "";
Fwrite ($ fp, $ out );
While (! Feof ($ fp ))
{
$ Ret. = fgets ($ fp, 128 );
}
Fclose ($ fp );
}
Return true;
}
?>
2. use PHP to POST data to the page
Function socketPost ($ url, $ data, & $ ret)
{
$ UrlArr = parse_url ($ url );
$ Host = $ urlArr ['host'];
$ Port = isset ($ urlArr ['port'])? $ UrlArr ['port']: 80;
$ Path = isset ($ urlArr ['path'])? $ UrlArr ['path']: "/";
$ Fp = fsockopen ($ host, $ port, $ errno, $ errstr, 30 );
If (! $ Fp)
{
Echo "$ errstr ($ errno)
\ N ";
Return false;
}
Else
{
$ Out = "POST $ path HTTP/1.1 \ r \ n ";
$ Out. = "Host: $ host \ r \ n ";
$ Out. = "Content-Type: application/x-www-form-urlencoded \ r \ n ";
$ Out. = "Content-Length:". strlen ($ data). "\ r \ n ";
$ Out. = "Connection: Keep-Alive \ r \ n ";
$ Out. = $ data;
$ Ret = "";
Fwrite ($ fp, $ out );
While (! Feof ($ fp ))
{
$ Ret. = fgets ($ fp, 128 );
}
Fclose ($ fp );
}
Return true;
}
?>
If post reports an error, change Keep-Alive in $ out. = "Connection: Keep-Alive \ r \ n"; to Close.

Excerpt from advancing with the times

Http://www.bkjia.com/PHPjc/478311.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478311.htmlTechArticle1. GET webpage content using PHP? Php function socketGet ($ url, $ ret) {$ urlArr = parse_url ($ url); $ host = $ urlArr [host]; $ port = isset ($ urlArr [port])? $ UrlArr [port]: 80; $ pa...

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.