PHP six ways to invoke a way URL

Source: Internet
Author: User
Tags http post
PHP six ways to invoke a remote URL

Example code 1: Get content with file_get_contents in get
???
$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
??
?? $fp = fopen ($url, ' R ');
? ?
?? Printarr (Stream_get_meta_da

Ta ($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

?? ?? $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
?? ),
?? );

$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
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\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;

}


?>

Example code 5: Open the URL with the Fsockopen function to get the full data in post, including the header and body
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

$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 dictfile, 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

function Printarr (array $arr)
{
??? echo "
Row field Count: ". Count ($arr)."
";
??? foreach ($arr as $key = $value)
??? {
????? ?
??????????? echo "$key = $value???
";
??? }
}
?>

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