Six methods for php to call remote URLs

Source: Internet
Author: User
Tags net domain
Php calls six remote url methods. For more information, see. Example code 1: Use file_get_contents to get content in get mode

The 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: Use fopen to open a url and get the content

The 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 );
?>


Sample Code 3: Use the file_get_contents function to obtain the url in post mode.

The 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 \ 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

The code is as follows:


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

The code is as follows:


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.

The 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 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

The code is as follows:


Functionprintarr (array $ arr)
{
Echo"
Row field count: ". count ($ arr )."
";
Foreach ($ arras $ key => $ value)
{
Echo "$ key = $ value
";
}
}
?>


========================================================== ====================
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:

The code is as follows:


/*
+ -------------------------------------------------------------
+ Capture the webpage title code, directly copy the code snippet, and save it as a. php file.
+ -------------------------------------------------------------
*/

Error_reporting (7 );
$ File = fopen ("http://www.jb51.net/", "r ");
If (! $ File ){
Echo "Unable to open remote file. \ n ";
Exit;
}
While (! Feof ($ file )){
$ Line = fgets ($ file, 1024 );
If (eregi ("(.*)", $ 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:

The code is as follows:


/*
+ ----------------
+ DNSing Sprider
+ ----------------
*/

$ Fp = fsockopen ("www.dnsing.com", 80, $ errno, $ errstr, 30 );
If (! $ Fp ){
Echo "$ errstr ($ errno)
\ 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.

// 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 = '/^ (# | javascript .*? | 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>)/I ";
Preg_match_all ($ regx, $ str, $ result );

If (count ($ result) = 3 ){
$ Result [2] = str_replace ("Multimedia: "," ", $ result [2]);
$ Result [2] = str_replace ("
"," ", $ 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.

----------------------------------------------------
Http://www.phpcake.cn/archives/tag/fsockopen
PHP provides multiple methods to obtain remote webpage content, such as using built-in functions such as file_get_contents and fopen.


Echo file_get_contents ("http://img.jb51.net/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 img.jb51.net is resolved to three IP addresses, namely 72.249.146.213, 72.249.146.214, and 72.249.146.215 by DNS. each time you access img.jb51.net, the system accesses one of the servers based on the corresponding.
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://img.jb51.net/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 img.jb51.net domain name in the header. So I wrote the following PHP function:

The code is as follows:



/************************
* Function purpose: obtain the remote webpage content of the specified server when the same domain name corresponds to multiple IP addresses.
* Creation time:
* Created By: Zhang Yan (img.jb51.net)
* Parameter description:
* $ Ip address of the ip server
* $ Host server host name
* $ Url server URL (excluding domain names)
* Return value:
* Remote webpage content obtained
* False: An error occurred while accessing the remote webpage.
************************/
Function HttpVisit ($ ip, $ host, $ url)
{
$ Errstr = '';
$ Errno = '';
$ Fp = fsockopen ($ ip, 80, $ errno, $ errstr, 90 );
If (! $ Fp)
{
Return false;
}
Else
{
$ Out = "GET {$ url} HTTP/1.1 \ r \ n ";
$ Out. = "Host: {$ host} \ r \ n ";
$ Out. = "Connection: close \ r \ n ";
Fputs ($ fp, $ out );

While ($ line = fread ($ fp, 4096 )){
$ Response. = $ line;
}
Fclose ($ fp );

// Remove Header information
$ Pos = strpos ($ response, "\ r \ n ");
$ Response = substr ($ response, $ pos + 4 );

Return $ response;
}
}

// Call method:
$ Server_info1 = HttpVisit ("72.249.146.213", "img.jb51.net", "/abc. php ");
$ Server_info2 = HttpVisit ("72.249.146.214", "img.jb51.net", "/abc. php ");
$ Server_info3 = HttpVisit ("72.249.146.215", "img.jb51.net", "/abc. php ");
?>

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.