Home > Backend Development > PHP Tutorial > Use fsocket to simulate GET and POST requests_PHP tutorial

Use fsocket to simulate GET and POST requests_PHP tutorial

WBOY
Release: 2016-07-13 17:52:29
Original
1138 people have browsed it

[php]
//fsocket simulates get submission
$gurl = "http://localhost/php/t.php?uu=gggggg";
//print_r(parse_url($gurl));
echo "The following is the response content in GET mode:
";
sock_get($gurl);
function sock_get($url)
{
$info = parse_url($url);
$fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
$head = "GET ".$info['path']."?".$info["query"]." HTTP/1.0rn";
$head .= "Host: ".$info['host']."rn";
$head .= "rn";
$write = fputs($fp, $head);
while (!feof($fp))
{
$line = fgets($fp);
echo $line."
";
}
}



//fsocket simulates post submission
$purl = "http://localhost/php/t.php";
echo "The following is the response content in POST mode:
";
sock_post($purl,"uu=rrrrrrrrrrr&&kk=mmmmmm");
function sock_post($url, $query)
{
$info = parse_url($url);
$fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
$head = "POST ".$info['path']." HTTP/1.0rn";
$head .= "Host: ".$info['host']."rn";
$head .= "Referer: http://".$info['host'].$info['path']."rn";
$head .= "Content-type: application/x-www-form-urlencodedrn";
$head .= "Content-Length: ".strlen(trim($query))."rn";
$head .= "rn";
$head .= trim($query);
$write = fputs($fp, $head);
while (!feof($fp))
{
$line = fgets($fp);
echo $line."
";
}
}
?>

Requested response page t.php
[php]
if(isset($_GET['uu'])){
echo 't.php The value of $_GET["uu"] is: '.$_GET['uu']."
";
}
if(isset($_POST['uu'])){
echo 'The value of $_POST int.php is:
';
Print_r($_POST);

?>

The following are the results:

Use fsocket to simulate GET and POST requests_PHP tutorial
The following are the results of Firebug:

Use fsocket to simulate GET and POST requests_PHP tutorial
Author: Fanteathy

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478114.htmlTechArticle[php] ?php //fsocket simulates get submission $gurl = http://localhost/php/t. php?uu=gggggg; //print_r(parse_url($gurl)); echo The following is the response content of GET method: br; sock_get($gurl); functio...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template