Six Methods for php to call remote URLs

Source: Internet
Author: User

Example code 1: Use file_get_contents to get content in get Mode
<? PHP
$ 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: Use fopen to open a URL and get the content
<?
$ 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 );
?>
Sample Code 3: Use the file_get_contents function to obtain the url in post mode.
<? Php
$ 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: Use the fsockopen function to open a url and get complete data, including header and body
<?
Functionget_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 ){
Returnfalse;
} Else {
$ Request = "get $ queryhttp/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 (! @ Feof ($ FP )){
$ Result. = @ fgets ($ FP, 1024 );
}
Fclose ($ FP );
Return $ result;
}
}
// Obtain the HTML part of the URL and remove the header
Functiongeturlhtml ($ URL, $ cookie = false ){
$ Rowdata = get_url ($ url, $ cookie );
If ($ rowdata)
{
$ Body = stristr ($ rowdata, "\ r \ n ");
$ Body = substr ($ body, 4, strlen ($ body ));
Return $ body;
}
Returnfalse;
}
?>
Example code 5: Use the fsockopen function to open the url and obtain the complete data in POST mode, including the header and body
<?
FunctionHTTP_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 ($ daTaas $ key => $ value)
$ Values [] = "$ key =". urlencode ($ value );
$ DaTa_string = implode ("&", $ values );

// Find out which port is 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: Use the curl library. before using the curl library, you may need to check php. ini to see if the curl extension has been enabled.
<?
$ 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 curl Library:
Http://curl.haxx.se/
Curl is a FILE Transfer tool using URL syntax. It supports FTP, FTPS, http htpps scp sftp tftp telnet dict file and LDAP. Curl supports SSL certificates, http post, http put, and FTP uploads, kerberos, HTT-based upload, proxy, cookie, user + password proof, file transfer recovery, http Proxy channel and a large number of other useful techniques
<?
Functionprintarr (array $ arr)
{
Echo "<br> Row field count:". count ($ arr). "<br> ";
Foreach ($ arras $ key => $ value)
{
Echo "$ key = $ value <br> ";
}
}
?>
========================================================== ====================
PHP code for capturing remote website data
Today, many programmers may have the same question: how to capture the HTML code of others' websites like search engines, and then collect and organize the code into useful data! Let me introduce some simple examples today.

I. Example of capturing a remote webpage title:
The following is a code snippet:
<? Php
/*
+ -------------------------------------------------------------
+ Capture the webpage title code, directly copy the code snippet, and save it as a. php file.
+ -------------------------------------------------------------
*/

Error_reporting (7 );
$ File = fopen ("http://www.dnsing.com/", "r ");
If (! $ File ){
Echo "<font color = red> Unable to open remote file. </font> \ n ";
Exit;
}
While (! Feof ($ file )){
$ Line = fgets ($ file, 1024 );
If (eregi ("<title> (. *) </title>", $ line, $ out )){
$ Title = $ out [1];
Echo "". $ title ."";
Break;
}
}
Fclose ($ file );

// End
?>
Ii. Example of capturing HTML code from a remote webpage:

The following is a code snippet:
<? Php
/*
+ ----------------
+ Dnsing sprider
+ ----------------
*/

$ Fp = fsockopen ("www.dnsing.com", 80, $ errno, $ errstr, 30 );
If (! $ FP ){
Echo "$ errstr ($ errno) <br/> \ n ";
} Else {
$ Out = "Get/HTTP/1.1 \ r \ n ";
$ Out. = "Host: www.dnsing.com \ r \ n ";
$ Out. = "connection: Close \ r \ n ";
Fputs ($ FP, $ out );
While (! Feof ($ FP )){
Echo fgets ($ FP, 128 );
}
Fclose ($ FP );
}
// End
?>
The above two code snippets are copied directly back to run to understand the effect. The above example is just a prototype of web page data capture. To make it more suitable for your own use, the situation varies. so let's take a good look at it here.

====================================

Functions that make a little sense are: get_content_by_socket (), get_url (), get_content_url (), and get_content_object functions. You may have some ideas.
<? Php

// Get all content url and save it to the file
Function get_index ($ save_file, $ prefix = "index _"){
$ Count = 68;
$ I = 1;
If (file_exists ($ save_file) @ unlink ($ save_file );
$ Fp = fopen ($ save_file, "a +") or die ("Open". $ save_file. "failed ");
While ($ I <$ count ){
$ Url = $ prefix. $ I. ". htm ";
Echo "Get". $ url ."...";
$ Url_str = get_content_url (get_url ($ url ));
Echo "OK \ n ";
Fwrite ($ fp, $ url_str );
++ $ I;
}
Fclose ($ fp );
}

// Obtain the target multimedia object
Function get_object ($ url_file, $ save_file, $ split = "| --: **: -- | "){
If (! File_exists ($ url_file) die ($ url_file. "not exist ");
$ File_arr = file ($ url_file );
If (! Is_array ($ file_arr) | empty ($ file_arr) die ($ url_file. "not content ");
$ Url_arr = array_unique ($ file_arr );
If (file_exists ($ save_file) @ unlink ($ save_file );
$ Fp = fopen ($ save_file, "a +") or die ("Open save file". $ save_file. "failed ");
Foreach ($ url_arr as $ url ){
If (empty ($ url) continue;
Echo "Get". $ url ."...";
$ Html_str = get_url ($ url );
Echo $ html_str;
Echo $ url;
Exit;
$ Obj_str = get_content_object ($ html_str );
Echo "OK \ n ";
Fwrite ($ fp, $ obj_str );
}
Fclose ($ fp );
}

// Retrieve the file content through the Directory
Function get_dir ($ save_file, $ dir ){
$ Dp = opendir ($ dir );
If (file_exists ($ save_file) @ unlink ($ save_file );
$ Fp = fopen ($ save_file, "a +") or die ("Open save file". $ save_file. "failed ");
While ($ file = readdir ($ dp ))! = False ){
If ($ file! = "." & $ File! = ".."){
Echo "Read file". $ file ."...";
$ File_content = file_get_contents ($ dir. $ file );
$ Obj_str = get_content_object ($ file_content );
Echo "OK \ n ";
Fwrite ($ FP, $ obj_str );
}
}
Fclose ($ FP );
}

// Obtain the content of the specified URL
Function get_url ($ URL ){
$ Reg = '/^ http: \/[^ \/]. + $ /';
If (! Preg_match ($ Reg, $ URL) Die ($ URL. "invalid ");
$ Fp = fopen ($ URL, "R") or die ("Open URL:". $ URL. "failed .");
While ($ fc = fread ($ FP, 8192 )){
$ Content. = $ FC;
}
Fclose ($ FP );
If (empty ($ content )){
Die ("Get URL:". $ URL. "content failed .");
}
Return $ content;
}

// Use socket to obtain the specified webpage
Function get_content_by_socket ($ URL, $ host ){
$ Fp = fsockopen ($ host, 80) or die ("open". $ URL. "failed ");
$ Header = "Get/". $ URL. "HTTP/1.1 \ r \ n ";
$ Header. = "accept: */* \ r \ n ";
$ Header. = "Accept-language: ZH-CN \ r \ n ";
$ Header. = "Accept-encoding: gzip, deflate \ r \ n ";
$ Header. = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1; Maxthon; infopath.1;. Net CLR 2.0.50727) \ r \ n ";
$ Header. = "Host:". $ host. "\ r \ n ";
$ Header. = "connection: keep-alive \ r \ n ";
// $ Header. = "Cookie: cnzz02 = 2; rtime = 1; ltime = 1148456424859; cnzz_eid = 56601755-\ r \ n ";
$ Header. = "connection: Close \ r \ n ";

Fwrite ($ FP, $ header );
While (! Feof ($ FP )){
$ Contents. = fgets ($ FP, 8192 );
}
Fclose ($ FP );
Return $ contents;
}

// Obtain the URL in the specified content
Function get_content_url ($ host_url, $ file_contents ){

// $ Reg = '/^ (# | javasForbidden .*? | Ftp: \/. + | http: \/. + | .*? Href .*? | Play .*? | Index .*? | .*? Asp) + $/I ';
// $ Reg = '/^ (down .*? \. Html | \ d + _ \ d + \. htm .*?) $/I ';
$ Rex = "/([hH] [rR] [eE] [Ff]) \ s * = \ s * ['\ "] * ([^>' \" \ s] +) [\ "'>] * \ s */I ";
$ Reg = '/^ (down .*? \. Html) $/I ';
Preg_match_all ($ rex, $ file_contents, $ r );
$ Result = ""; // array ();
Foreach ($ r as $ c ){
If (is_array ($ c )){
Foreach ($ c as $ d ){
If (preg_match ($ reg, $ d) {$ result. = $ host_url. $ d. "\ n ";}
}
}
}
Return $ result;
}

// Obtain the multimedia file in the specified content
Function get_content_object ($ str, $ split = "| --: **: -- | "){
$ Regx = "/href \ s * = \ s * ['\"] * ([^>' \ "\ s] +) [\ "'>] * \ s * (<B>. *? <\/B>)/I ";
Preg_match_all ($ regx, $ str, $ result );

If (count ($ result) = 3 ){
$ Result [2] = str_replace ("<B> Multimedia:", "", $ result [2]);
$ Result [2] = str_replace ("</B>", "", $ result [2]);
$ Result = $ result [1] [0]. $ split. $ result [2] [0]. "\ n ";
}
Return $ result;
}

?>

========================================================== ====================

When the same domain name corresponds to multiple IP addresses, PHP obtains the remote webpage content Function

FGC is simple to read and encapsulates all operations.
Fopen is also encapsulated, but you need to read all the data cyclically.
Fsockopen: This is the socket operation of the direct board. FGC is better if you only want to read an HTML page. If the company uses a firewall to access the Internet, the general file_get_content function will not work. Of course, it is also possible to write HTTP requests directly to the proxy through some socket operations, but it is troublesome.

If you can confirm that the file is small, you can choose the above two methods fopen, join ('', file ($ file ));. For example, if you only operate files smaller than 1 kb, you 'd better use file_get_contents.

If it is determined that the file is large or the file size cannot be determined, it is best to use the file stream. There is no obvious difference between a 1 K fopen file and a 1g fopen file. If the content is long, it can take a longer time to read, rather than let the script die.

---------------------------------------------------- Success.
 

<? Php

echo file_get_contents("http://blog.s135.com/abc.php");?>

However, in Server Load balancer such as DNS round-robin, the same domain name may correspond to multiple servers and multiple IP addresses. Assume that blog.s135.com is resolved by DNS to three IP addresses, namely 72.249.146.213, 72.249.146.214, and 72.249.146.215. Each time you access blog.s135.com, the system accesses one of the servers based on the server.

When I was working on a video project last week, I had to access a PHP Interface Program (Suppose abc. php) on each server in sequence to query the transmission status of this server.

In this case, you cannot directly use file_get_contents to access http://blog.s135.com/abc.php. In this case, a server can be re-deployed directly.

Access http: // 72.249.146.213/abc in sequence. php, http: // 72.249.146.214/abc. php, http: // 72.249.146.215/abc. the php method does not work when the Web Server on the three servers has multiple virtual hosts.

You cannot set the local hosts because hosts cannot set multiple IP addresses to correspond to the same domain name.

It can only be implemented through PHP and HTTP: when accessing abc. php, add the blog.s135.com domain name in the header. So I wrote the following PHP function:

<? Php

  1. /************************
  2. * Function purpose: Obtain the remote webpage content of the specified server when the same domain name corresponds to multiple IP addresses.
  3. * Creation Time:
  4. * Created by: Zhang Yan (blog.s135.com)
  5. * Parameter description:
  6. * $ Ip address of the ip server
  7. * $ Host server host name
  8. * $ Url server URL (excluding domain names)
  9. * Return value:
  10. * Remote webpage content obtained
  11. * False: An error occurred while accessing the remote webpage.
  12. ************************/
  13. Function HttpVisit ($ ip, $ host, $ url)
  14. {
  15. $ Errstr = '';
  16. $ Errno = '';
  17. $ Fp = fsockopen ($ ip, 80, $ errno, $ errstr, 90 );
  18. If (! $ Fp)
  19. {
  20. Return false;
  21. }
  22. Else
  23. {
  24. $ Out = "GET {$ url} HTTP/1.1 \ r \ n ";
  25. $ Out. = "Host: {$ host} \ r \ n ";
  26. $ Out. = "Connection: close \ r \ n ";
  27. Fputs ($ fp, $ out );
  28. While ($ line = fread ($ fp, 4096 )){
  29. $ Response. = $ line;
  30. }
  31. Fclose ($ fp );
  32. // Remove Header information
  33. $ Pos = strpos ($ response, "\ r \ n ");
  34. $ Response = substr ($ response, $ pos + 4 );
  35. Return $ response;
  36. }
  37. }
  38. // Call method:
  39. $ Server_info1 = httpvisit ("72.249.146.213", "blog.s135.com", "/ABC. php ");
  40. $ Server_info2 = httpvisit ("72.249.146.214", "blog.s135.com", "/ABC. php ");
  41. $ Server_info3 = httpvisit ("72.249.146.215", "blog.s135.com", "/ABC. php ");
  42. ?>

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.