Skip to content

Instantly share code, notes, and snippets.

@hatzopoulos
Last active August 29, 2015 14:13
Show Gist options
  • Save hatzopoulos/2b23577d6fe89e35f890 to your computer and use it in GitHub Desktop.
Save hatzopoulos/2b23577d6fe89e35f890 to your computer and use it in GitHub Desktop.
Miscellaneous PHP Functions
<?php
function command_exists($cmd) {
return shell_exec('command -v '.$cmd.' >/dev/null && echo 1 || echo 0');
}
<?php
// Tries to get only the URL's HEAD contents and not the body.
function file_get_head($url) {
$ch = curl_init();
$max_redirects = 5;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $max_redirects > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $max_redirects);
curl_setopt($ch, CURLOPT_HEADER, true); // TRUE to include the header in the output.
curl_setopt($ch, CURLOPT_NOBODY, true); // TRUE to exclude the body from the output. Request method is then set to HEAD. Changing this to FALSE does not change it to GET.
curl_setopt($ch, CURLOPT_FORBID_REUSE, true); // TRUE to force the connection to explicitly close when it has finished processing, and not be pooled for reuse.
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); // TRUE to force the use of a new connection instead of a cached one
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($ch, CURLOPT_URL, $url);
$header = curl_exec($ch);
if (curl_errno($ch)) {
$code = 0;
}
else {
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
}
return array('code' => $code, 'header' => $header);
}
<?php
// Taken from http://stackoverflow.com/a/7798842
function mempty() {
foreach (func_get_args() as $arg) {
if (empty($arg)) {
continue;
}
else {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment