Using PHP to implement Get,post data _php tutorials

Source: Internet
Author: User
1. Get the Web content get by 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\r\n";
$ret = "";
Fwrite ($fp, $out);
while (!feof ($FP))
{
$ret. = Fgets ($fp, 128);
}
Fclose ($FP);
}
return true;
}
?>
2. POST data to the page using PHP
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\r\n";
$out. = $data;
$ret = "";
Fwrite ($fp, $out);
while (!feof ($FP))
{
$ret. = Fgets ($fp, 128);
}
Fclose ($FP);
}
return true;
}
?>
If post is an error, turn $out = "connection:keep-alive\r\n\r\n"; keep-alive in to close

Excerpt from The Times

http://www.bkjia.com/PHPjc/478311.html www.bkjia.com true http://www.bkjia.com/PHPjc/478311.html techarticle 1. Get the Web content by 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.