Skip to content

Instantly share code, notes, and snippets.

@shorti1996
Created January 3, 2019 12:09
Show Gist options
  • Save shorti1996/8c1f58173b852dd63c36c729880e16e9 to your computer and use it in GitHub Desktop.
Save shorti1996/8c1f58173b852dd63c36c729880e16e9 to your computer and use it in GitHub Desktop.
Ping and request in php
<?php
set_time_limit(4);
ob_start();
$file = 'logs.txt';
$date_run = date(DATE_ATOM);
function shutdown () {
global $file;
$out = ob_get_clean();
echo "<div>out: <div style='color: blue;'>$out</div></div>";
file_put_contents($file, "KILLED\n\n\n", FILE_APPEND | LOCK_EX);
}
register_shutdown_function('shutdown');
function ping($host, $port, $timeout) {
$tB = microtime(true);
$fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
if (!$fP) { return "down"; }
$tA = microtime(true);
return round((($tA - $tB) * 1000), 0)." ms";
}
file_put_contents($file, $date_run."\n", FILE_APPEND | LOCK_EX);
//echo '<div>max_execution_limit: ' . ini_get('max_execution_time') . '</div>';
//Echoing it will display the ping if the host is up, if not it'll say "down".
$ping_result = ping("gusto.boleslawiec.pl", 80, 10);
file_put_contents($file, $ping_result."\n", FILE_APPEND | LOCK_EX);
//$curl = curl_init('http://testcURL.com');
//$result = curl_exec($curl);
//curl_close();
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
//CURLOPT_URL => 'http://testcURL.com/?item1=value&item2=value2',
CURLOPT_URL => 'gusto.boleslawiec.pl',
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Connection: keep-alive',
'Cache-Control: max-age=0',
'Upgrade-Insecure-Requests: 1',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding: gzip, deflate',
'Accept-Language: pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Show me what you got
$htmls = htmlentities($resp);
file_put_contents($file, $htmls."\n\n\n", FILE_APPEND | LOCK_EX);
// Close request to clear up some resources
curl_close($curl);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment