Home > Backend Development > PHP Tutorial > Using socket method to get and POST data examples in PHP, socketget_PHP tutorial

Using socket method to get and POST data examples in PHP, socketget_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 09:58:11
Original
844 people have browsed it

Use socket method GET and POST data examples in PHP, socketget

1. Use PHP to obtain web page content GET method
Copy code The code is as follows:
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.1rn";
$out .= "Host: $hostrn";
$out .= "Connection: Closernrn";
$ret = "";
fwrite($fp, $out);
While (!feof($fp))
{
            $ret .= fgets($fp, 128);
}
       fclose($fp);
}
return true;
}
?>

2. Use PHP to POST data to the page
Copy code The code is as follows:
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.1rn";
$out .= "Host: $hostrn";
$out .= "Content-Type: application/x-www-form-urlencodedrn";
$out .= "Content-Length: ".strlen($data)."rn";
$out .= "Connection: Keep-Alivernrn";
$out .= $data;
$ret = "";
fwrite($fp, $out);
While (!feof($fp))
{
            $ret .= fgets($fp, 128);
}
       fclose($fp);
}
return true;
}
?>

If the post reports an error, change Keep-Alive in $out .= "Connection: Keep-Alivernrn"; to Close

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/978385.htmlTechArticleUsing socket method to get and POST data examples in PHP, socketget 1. Use PHP to obtain web page content GET method copy code code As follows: php function socketGet($url, nbsp;$urlArr = parse_u...
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template