Skip to content

Instantly share code, notes, and snippets.

@0x00000FF
Created July 5, 2021 03:33
Show Gist options
  • Save 0x00000FF/ef07ca2bd895d9e29d0f98784d73de84 to your computer and use it in GitHub Desktop.
Save 0x00000FF/ef07ca2bd895d9e29d0f98784d73de84 to your computer and use it in GitHub Desktop.
PHP webhook for github
<?php
// utility
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
// acquire package
$acctoken = "[REDACTED_FOR_TOKEN_SECURITY]";
$userid = "";
$repository = "";
$branch = "master";
$fcontext = fopen('package.zip', 'wb');
$context = curl_init();
curl_setopt_array($context, [
CURLOPT_URL => "https://api.github.com/repos/$userid/$repository/zipball/$branch",
CURLOPT_HTTPHEADER => [
"Authorization: token $acctoken",
"User-Agent: WebhookEndpoint"
],
CURLOPT_FILE => $fcontext,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
]);
curl_exec($context);
curl_close($context);
fclose($fcontext);
// unzip and update package
$zip = new ZipArchive;
$zip_res = $zip->open("package.zip");
if ($zip_res) {
$zip->extractTo("package_unzip");
$zip->close();
} else {
echo "failed";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment