PHP 6 ways to send get, POST request concise summary _php Example

Source: Internet
Author: User

Method 1: Get the content with file_get_contents:

<?php
$url = ' http://www.jb51.net/';
$html = file_get_contents ($url);
echo $html;
? >

Method 2: Open the URL with fopen to get the content:

<?php
$fp = fopen ($url, ' R ');
Stream_get_meta_data ($FP);
while (!feof ($fp)) {
$result. = Fgets ($fp, 1024);
}
echo "URL body: $result";
Fclose ($FP);
? >

Method 3: Use the file_get_contents function to get the URL by post

<?php
$data = Array (' foo ' => ' Bar ');
$data = Http_build_query ($data);

$opts = Array ('
http ' => Array (
' method ' => ' POST ',
' header ' => ') content-type:application/ X-www-form-urlencodedrn ".
" Content-length: ". Strlen ($data). "RN",
' content ' => $data
)
);

$context = Stream_context_create ($opts);
$html = file_get_contents (' http://localhost/e/admin/test.html ', false, $context);

echo $html;
? >

Method 4: Open the URL with the Fsockopen function to get the complete data, including header and Body,fsockopen need php.ini allow_url_fopen option open

<?php
function Get_url ($url, $cookie =false)
{
$url = Parse_url ($url);
$query = $url [path]. $url [query];
echo "Query:". $query;
$fp = Fsockopen ($url [host], $url [port]? $url [port]:80, $errno, $errstr,);
if (! $fp) {return
false;
} else {
$request = ' Get $query http/1.1rn ';
$request. = "Host: $url [host]rn";
$request. = "Connection:closern";
if ($cookie) $request. = "Cookie:  $cookien";
$request. = "RN";
Fwrite ($fp, $request);
while (! @feof ($fp)) {
$result. = @fgets ($fp, 1024);
Fclose ($FP);
return $result;
}
Gets the HTML portion of the URL, removing the header
function geturlhtml ($url, $cookie =false)
{
$rowdata = Get_url ($url, $cookie) ;
if ($rowdata)
{
$body = stristr ($rowdata, "rnrn");
$body =substr ($body, 4,strlen ($body));
return $body;
}

return false;
>

Method 5: Open the URL with the Fsockopen function to get the complete data in post, including header and body

<?php function Http_post ($URL, $data, $cookie, $referrer = "") {//parsing the given URL $URL _info=parse_url ($URL);

Building referrer if ($referrer = = "")//if not given use this script as referrer $referrer = "111″;
Making string from $data foreach ($data as $key => $value) $values []= "$key =". UrlEncode ($value);

$data _string=implode ("&", $values);

Find out which the port is needed–if to given use standard (=80) if (!isset ($URL _info["Port")) $URL _info["Port"]=80; Building post-request: $request. = "POST". $URL _info["path"]. "
HTTP/1.1N "; $request. = "Host:". $URL _info["host"]. "
n ";
$request. = "Referer: $referern";
$request. = "Content-type:application/x-www-form-urlencodedn"; $request. = "Content-length:". strlen ($data _string). "
n ";

$request. = "Connection:closen";

$request. = "Cookie: $cookien";
$request. = "n"; $request. = $data _string. "

n ";
$fp = Fsockopen ($URL _info["host"), $URL _info["Port");
Fputs ($fp, $request);

while (!feof ($fp)) {$result. = fgets ($fp, 1024);} fclose ($FP); RetuRN $result; }?>

Method 6: Use the Curl Library, before using the Curl library, you might want to see if PHP.ini has opened the curl extension

<?php
$ch = Curl_init ();
$timeout = 5;
curl_setopt ($ch, Curlopt_url, ' http://www.jb51.net/');
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
$file _contents = curl_exec ($ch);
Curl_close ($ch);

echo $file _contents;
? >

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.