Summary of several methods of opening URL address under PHP _php tutorial

Source: Internet
Author: User
Tags http post
1: Get content with file_get_contents in get mode
Copy CodeThe code is as follows:
$url = ' http://www.baidu.com/';
$html = file_get_contents ($url);
Print_r ($http _response_header);
EC ($html);
Printhr ();
Printarr ($http _response_header);
Printhr ();
?>

Example code 2: Open URL with fopen, get content in Get
Copy CodeThe code is as follows:
$fp = fopen ($url, ' R ');
Printarr (Stream_get_meta_data ($fp));
Printhr ();
while (!feof ($fp)) {
$result. = Fgets ($fp, 1024);
}
echo "URL body: $result";
Printhr ();
Fclose ($FP);
?>

Example Code 3: Get the URL by post using the File_get_contents function
Copy CodeThe code is as follows:
$data = Array (' foo ' = ' bar ');
$data = Http_build_query ($data);
$opts = Array (
' http ' = = Array (
' Method ' = ' POST ',
' Header ' = ' content-type:application/x-www-form-urlencoded '.
"Content-length:". Strlen ($data). "",
' Content ' = $data
),
);
$context = Stream_context_create ($opts);
$html = file_get_contents (' http://localhost/e/admin/test.html ', false, $context);
Echo $html;
?>

Example Code 4: Open the URL with the Fsockopen function and get the full data in Get mode, including header and body
Copy CodeThe code is as follows:
function Get_url ($url, $cookie =false) {
$url = Parse_url ($url);
$query = $url [path]. "?". $url [query];
EC ("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";
$request. = "Host: $url [host]";
$request. = "Connection:close";
if ($cookie) $request. = "Cookie: $cookie \ n";
$request. = "";
Fwrite ($fp, $request);
while (! @feof ($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, "");
$body =substr ($body, 4,strlen ($body));
return $body;
}
return false;
}

?>

Example Code 5: Open the URL with the Fsockopen function to get the full data in post, including the header and body
Copy CodeThe code is as follows:
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;
}
Printhr ();
?>

Example code 6: Using the Curl Library, before using the Curl library, you might need to look at php.ini to see if the curl extension has been turned on
Copy CodeThe code is as follows:
$ch = Curl_init ();
$timeout = 5;
curl_setopt ($ch, Curlopt_url, ' http://www.baidu.com/');
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
$file _contents = curl_exec ($ch);
Curl_close ($ch);
echo $file _contents;
?>

About the Curl Library:
Curl Official website http://curl.haxx.se/
Curl is a routing file tool that uses URL syntax to support FTP, FTPS, HTTP htpps SCP SFTP TFTP, TELNET DICT file, and LDAP. Curl supports SSL certificates, HTTP POST, http PUT, FTP uploads, Kerberos, HTT-based uploads, proxies, cookies, user + password proofs, file transfer recovery, HTTP proxy channels, and a number of other useful tricks
Copy CodeThe code is as follows:
function Printarr (array $arr)
{
echo "
Row field Count: ". Count ($arr)."
";
foreach ($arr as $key = $value)
{
echo "$key = $value
";
}
}
?>

7.
Some hosting providers have PHP's allow_url_fopen option turned off, and it is impossible to use file_get_contents directly to get the content of a remote Web page. That is, you can use another function, curl.
Here are the different ways to do the same function for file_get_contents and curl two functions
Example of using the File_get_contents function:
Copy CodeThe code is as follows:
< PHP
$file _contents = file_get_contents (' http://www.ccvita.com/');
echo $file _contents;
?>

Use an example of a curl function:
Copy CodeThe code is as follows:
< PHP
$ch = Curl_init ();
$timeout = 5;
curl_setopt ($ch, Curlopt_url, ' http://www.ccvita.com ');
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
$file _contents = curl_exec ($ch);
Curl_close ($ch);
echo $file _contents;
?>

using the Function_exists function to determine whether PHP supports a function can easily write down the following function
Copy CodeThe code is as follows:
< PHP
function Vita_get_url_content ($url) {
if (function_exists (' file_get_contents ')) {
$file _contents = file_get_contents ($url);
} else {
$ch = Curl_init ();
$timeout = 5;
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
$file _contents = curl_exec ($ch);
Curl_close ($ch);
}
return $file _contents;
}
?>

In fact, the above function is debatable, if your host service file_get_contents and curl are closed, the above function will appear error.

http://www.bkjia.com/PHPjc/321918.html www.bkjia.com true http://www.bkjia.com/PHPjc/321918.html techarticle 1: Use file_get_contents to get the content copy code code as follows: PHP $url = ' http://www.baidu.com/'; $html = file_get_contents ($url); Print_r ($http _response_header); EC ...

  • 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.