PHP get Web content Method Summary

Source: Internet
Author: User

Method 1: Use file_get_contents to get the content in the Get mode
<?php
$url = ' http://www.domain.com/?para=123 ';
$html = file_get_contents ($url);
Echo $html;
?>

Method 2: Use the file_get_contents function to get the URL by post
<?php
$url = ' http://www.domain.com/test.php?id=123 ';
$data = Array (' foo ' = ' bar ');
$data = Http_build_query ($data);

$opts = Array (
' http ' = = Array (
' Method ' = ' POST ',
' Header ' = ' content-type:application/x-www-form-urlencoded\r\n '.
"Content-length:". Strlen ($data). "\ r \ n",
' Content ' = $data
)
);
$ctx = Stream_context_create ($opts);
$html = @file_get_contents ($url, ", $ctx);

If you need to pass the cookie data again,
' Header ' = ' content-type:application/x-www-form-urlencoded\r\n '.
"Content-length:". Strlen ($data). "\ r \ n",
Revision changed to
' Header ' = ' content-type:application/x-www-form-urlencoded\r\n '.
"Content-length:". Strlen ($data). "\ r \ n".
"Cookie:cookie1=c1;cookie2=c2\r\n";
Can

Method 3: Open the URL with fopen and get the content in Get mode
<?php
$fp = fopen ($url, ' R ');
$header = Stream_get_meta_data ($FP);//Get header information
while (!feof ($fp)) {
$result. = Fgets ($fp, 1024);
}
echo "URL header: {$header} <br>":
echo "URL body: $result";
Fclose ($FP);
?>

Method 4: Open the URL with fopen and post to get the content
<?php
$data = Array (' foo2 ' = ' bar2 ', ' foo3 ' = ' bar3 ');
$data = Http_build_query ($data);

$opts = Array (
' http ' = = Array (
' Method ' = ' POST ',
' Header ' = ' content-type:application/x-www-form-urlencoded\r\ncookie:cook1=c3;cook2=c4\r\n '.
"Content-length:". Strlen ($data). "\ r \ n",
' Content ' = $data
)
);

$context = Stream_context_create ($opts);
$html = fopen (' http://www.test.com/zzzz.php?id=i3&id2=i4 ', ' RB ', false, $context);
$w =fread ($html, 1024);
echo $w;
?>

Method 5: Open the URL with the Fsockopen function and get the full data in Get mode, including header and body
<?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, 30);
if (! $fp) {
return false;
} else {
$request = "GET $query http/1.1\r\n";
$request. = "Host: $url [host]\r\n";
$request. = "connection:close\r\n";
if ($cookie) $request. = "Cookie: $cookie \ n";
$request. = "\ r \ n";
Fwrite ($fp, $request);
while ([email protected] ($FP)) {
$result. = @fgets ($fp, 1024);
}
Fclose ($FP);
return $result;
}
}
Gets the HTML part of the URL, removing the header
function geturlhtml ($url, $cookie =false)
{
$rowdata = Get_url ($url, $cookie);
if ($rowdata)
{
$body = Stristr ($rowdata, "\r\n\r\n");
$body =substr ($body, 4,strlen ($body));
return $body;
}

return false;
}
?>

Method 6: Use the Fsockopen function to open the URL, post to get the full data, 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 with 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 port was needed-if not given use standard (=80)
if (!isset ($URL _info["Port"))
$URL _info["Port"]=80;

Building Post-request:
$request. = "POST". $URL _info["path"]. " Http/1.1\n ";
$request. = "Host:". $URL _info["host"]. " \ n ";
$request. = "Referer: $referer \ n";
$request. = "content-type:application/x-www-form-urlencoded\n";
$request. = "Content-length:". strlen ($data _string). " \ n ";
$request. = "connection:close\n";

$request. = "Cookie: $cookie \ n";

$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 7: Using the Curl Library, before using the Curl Library, you may need to see if php.ini has the curl extension turned on
<?php

$url = "Http://172.168.1.16:8080/tongi/user/mobileValidation.do";
$post _data = Array ("mobile" = ' {$mobile} ');
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
Post data
curl_setopt ($ch, Curlopt_post, 1);
Variables for post
curl_setopt ($ch, Curlopt_postfields, $post _data);
$output = curl_exec ($ch);
Curl_close ($ch);
Echo $output;

?>

PHP get Web content Method Summary

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.