PHP Salt


A little dash of PHP... my collection of php functions

Version: 2.33.2
Last Build: December 29, 2023 20:28pm (PST)

Main Menu

Home

Get PHP Salt


The Functions

array
db
file
graph
html
mail
math
misc
mobile
script
string
system
time
web
psFileGetContents()
Reads file/url into String


Description:

This is used to replace file_get_contents()

Just a wrapper if local file, but uses CURL if a http(s) url

updated: 2.20.6
To speed things up, we reuse the curl object and this fixed
the issue sometimes with servers that have keep-alive on, it
would take a while to clsoe connection.




Usage:


$varFloat = psFileGetContents(URL|FILE,FLAG_TRIM);

FLAG_TRIM
true - the default, uses trim() on the return string
false - do not trim the return string (usefull for binary data)




Example:


$strPage = psFileGetContents("http://www.google.com");




Code:



function psFileGetContents($wFile, $flgTrim=true)
{
global $_PHPSALT;

if (substr(strtolower($wFile),0,4) == "http")
{
// curl reuse added
if ( $_PHPSALT['global/curl'] == false )
{
$ch = curl_init();
$_PHPSALT['global/curl'] = $ch;
}
else
{
$ch = $_PHPSALT['global/curl'];
}

if ( $_PHPSALT['proxy'] !="" ) {
list($pHost,$pPort) = explode(":",$_PHPSALT['proxy']);
curl_setopt($ch, CURLOPT_PROXY, $pHost);
curl_setopt($ch, CURLOPT_PROXYPORT, $pPort);
}

curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_URL, $wFile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING,'');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$buffer = curl_exec($ch);
//curl_close($ch);
}
else
{

if (@$proxy !="") {
stream_context_set_default(['http'=>[ 'proxy'=>$_PHPSALT['proxy'] ]]);
}
$buffer = @file_get_contents($wFile);
}


if ($flgTrim == true) { $buffer = trim($buffer); }
return $buffer;
}


Changlelog:

02-25-2019 - added option for proxy


See Also

  psFile_gc()
And a shot out to:

PHP - php.net
Fedora Server - getfedora.com
Shameless ads to pay for site