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