GitHub PHP WebHook

So I just wrote my first webhook. Pretty exciting.

Here it is in all its glory. Post-commit from GitHub repo.

<?php
// Decode json payload
$payload = $_POST['payload'];
$data = json_decode(stripslashes($payload));
 
// read commit messages and forward '# ...' messages to twitter clone
if(is_object($data) && is_array($data->commits)) {
    foreach($data->commits as $commit) {
        if($commit->author->name == 'KevBurnsJr' 
        && substr($commit->message, 0, 1) == '#') {
 
            $url = 'http://user:pass@tools.companyname.com/twitter_clone.php';
 
            // form fields
            $data = array(
                'headline' => substr($commit->message, 2),
                'hightlight' => 0,
                'workgroup' => 5,
                'submit' => 'Save',
                'userid' => 4
            );
 
            do_post_request($url, http_build_query($data));
        }
    } 
} else {
    die('no data');
}
 
// This function makes the requests.  
// http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
function do_post_request($url, $data, $optional_headers = null) {
    $params = array('http' => array(
        'method' => 'POST',
        'content' => $data
    ));
    if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
    }
    $ctx = stream_context_create($params);
    $fp = @fopen($url, 'rb', false, $ctx);
    $response = @stream_get_contents($fp);
    return $response;
}

No Comment

No comments yet

Leave a reply