curl fsocket file_in phpget_Comparison of the Use of Three content Functions

  • 2021-06-28 08:55:30
  • OfStack

Grab remote content, previously using file_get_The content function, which was known for a long time to have such a nice thing as curl, is a bit complicated to use after a glance, without file_get_content is so simple and needs are not great, so I did not learn to use curl.
file_was not found until recently when a web theft program was being runget_content is no longer sufficient.I think when reading remote content, file_get_content is no better than curl except for its convenience.

curl and file_in phpget_Some comparisons of content

Main differences:

Learning revealed that curl supports many protocols, including FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE and LDAP, that is, it can do many file_get_What content can't do.curl can acquire and collect content remotely at php.Implement FTP upload and download for PHP web version;Implement simulated landing;Implement interface docking (API), data transfer;Implement simulation Cookie;Download file breakpoint continuation and so on, 10 points powerful.

Understanding the basic usage of curl1, I find it is not difficult. Just remember some of the settings, it is difficult to figure out a point, but we can just remember a few commonly used ones.

Turn on curl:

Because PHP does not support curl by default, if you want to use curl, you first need to turn it on in php.ini, that is, remove it.extension= php_The semicolon before curl.dll is saved, then restart apache/iis.

Basic grammar:


$my_curl = curl_init();    // Initialization 1 individual curl object 
curl_setopt($my_curl, CURLOPT_URL, "https://www.ofstack.com");    // Set what you need to capture URL
curl_setopt($my_curl,CURLOPT_RETURNTRANSFER,1);    // Set whether the result will be saved in a string or output to the screen. 1 Indicates that the result is saved to a string 
$str = curl_exec($curl);    // Execute Request 
echo $str;    // Output Capture Result 
curl_close($curl);    // Close url request 

Recently, I need to get music data from other people's websites.file_usedget_The contents function, however, always encounters problems with acquisition failures, and although timeouts are set according to the examples in the manual, they do not work most of the time:

$config['context'] = stream_context_create(array('http' = > array('method' = > "GET",
'timeout' = > 5//This timeout is unstable and often does not work
)
));

At this time, looking at the connection pool of the server, I will find a stack of similar errors, which gives me a headache:
file_get_contents (http://***): failed to open stream...

Instead of using the curl library, you have written a function replacement:
function curl_file_get_contents($durl){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $durl);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}

So, except for the real network problems, there are no more problems.
This is what someone else has done about curl and file_get_Testing of contents:
file_get_contents takes seconds to grab google.com:

2.31319094
2.30374217
2.21512604
3.30553889
2.30124092

Time used by curl:

0.68719101
0.64675593
0.64326
0.81983113
0.63956594

Is there a big gap?Heh, from the experience I used, these two tools differ not only in speed but also in stability.

It is recommended that friends with high stability requirements for network data capture use the curl_above.file_get_contents function, not only stable and fast, but also fake browser to deceive the target address!

Method 1: Use file_get_contents Gets Content as get


<?php
$url='http://www.domain.com/';
$html = file_get_contents($url);
echo $html;
?>

Method 2: Open url with fopen and get the content by get


<?php
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
?>

Method 3: Use file_get_contents function, obtained as post


<?php
$data = array ('foo' => 'bar');
$data = http_build_query($data);
$opts = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencodedrn" .
"Content-Length: " . strlen($data) . "rn",
'content' => $data
)
);
$context = stream_context_create($opts);
$html = file_get_contents('http://localhost/e/admin/test.html', false, $context);
echo $html;
?>

Method 4: Open url with the fsockopen function to obtain complete data by get, 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.1rn";
$request .= "Host: $url[host]rn";
$request .= "Connection: Closern";
if($cookie) $request.="Cookie: $cookien";
$request.="rn";
fwrite($fp,$request);
while()) {
$result .= @fgets($fp, 1024);
}
fclose($fp);
return $result;
}
}
// Obtain url Of html Part, remove header
function GetUrlHTML($url,$cookie=false)
{
$rowdata = get_url($url,$cookie);
if($rowdata)
{
$body= stristr($rowdata,"rnrn");
$body=substr($body,4,strlen($body));
return $body;
}
return false;
}
?>

Method 5: Open url with fsockopen function to get complete data, including header and body, by POST.


<?php
function HTTP_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($data as $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.1n";
$request.="Host: ".$URL_Info["host"]."n";
$request.="Referer: $referern";
$request.="Content-type: application/x-www-form-urlencodedn";
$request.="Content-length: ".strlen($data_string)."n";
$request.="Connection: closen";
$request.="Cookie: $cookien";
$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 6: Before using the curl library, you may need to check if php.ini has opened the curl extension under one context before using the curl library.


<?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.domain.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>

curl, fsockopen, file_in phpget_All three functions of contents can be used to collect simulated speeches.What's the difference between them, or are they particular?

Zhao Yongbin:
Sometimes file_is usedget_contents() calls external files and tends to time out errors.Replace with curl. The reason is not clear
curl is more efficient than file_get_contents () and fsockopen () are a little higher because CURL automatically caches DNS information (highlights await my personal observation).

Fan Jiapeng:
file_get_contents curl fsockopen
Selective operations in the current requested environment are not summarized in 1:
In view of our company's development of KBI applications:
Start with: file_get_contents
Later adopted: fsockopen
Last to date: curl

(Remote) What I personally understand is the following (not to mention, not to mention, please add)
file_get_contents requires php.ini to turn on allow_url_fopen, request http using http_fopen_wrapper, not keeplive.curl is possible.
file_get_contents() single execution is efficient and returns headless information.
This is fine when reading a normal file, but it can cause problems when reading remote files.
If you want to make a continuous connection, request multiple pages.So file_get_There will be problems with contents and fopen.
You may also get something wrong.So when you do some similar collections, there must be problems.
sock is more difficult to operate because of its cumbersome configuration.Return complete information.

Pan Shaoning-Tencent:
file_get_contents can get the content of an URL, but it can not post get.
curl can be post and get.head information is also available
socket is lower.You can set up interaction based on UDP or TCP protocol
file_get_contents and curl can do both, and socket can do both.
socket can do, curl may not do 1
file_get_contents spends more time just pulling data.Efficiency is high and simple.
I have also encountered this situation in Zhao. I set host through CURL to OK.This is related to the network environment


Related articles: