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
psWebGet()
Web Get


Description:

This is used to replace file_get_contents() for urls that need
to pass parameters

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

note: on update 2.20.6, we reuse the curl object and this fixed
the issue sometimes with servers that have keep-alive on, it
would take a while to close connection.




Usage:


$buffer = psWebGet(URL, [ARRY_OPT]);

(desc in progress)



Example:


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




Code:


function psWebGet($url, $opt=array() )
{
global $_PHPSALT;

// 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);
}

$opt['headers'][] = "X-FN: phpSalt psWebGet";
if ($opt['accept'] != "") { $opt['headers'][] = "Accept: {$opt['accept']}"; }
if ($opt['bearer'] != "") { $opt['headers'][] = "Authorization: Bearer {$opt['bearer']}"; }

curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
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);
curl_setopt($ch, CURLOPT_HTTPHEADER, $opt['headers']);
$buffer = curl_exec($ch);
//curl_close($ch);

//dprint_pre($opt['headers']);

if ($opt['trim'] == true) { $buffer = trim($buffer); }
return $buffer;
}


Changlelog:

11-10-2021 - added to phpsalt


See Also

And a shot out to:

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