Skip to content

Instantly share code, notes, and snippets.

@EDDYMENS
Last active April 24, 2019 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EDDYMENS/eb92e22996effa42ec43b9b794d2a5e4 to your computer and use it in GitHub Desktop.
Save EDDYMENS/eb92e22996effa42ec43b9b794d2a5e4 to your computer and use it in GitHub Desktop.
Single file (pusher.com) PHP SDK
<?php
function push(string $region, string $app_id, string $app_key, string $app_secret, string $channel, string $event, string $data) {
$postData = json_encode(['data'=>$data, 'name'=>$event, 'channel'=>$channel]);
$md5_string = md5($postData);
$timestamp = strtotime('now +5 minutes');
$auth_signature = hash_hmac('sha256', "POST\n/apps/$app_id/events\nauth_key=$app_key&auth_timestamp=$timestamp&auth_version=1.0&body_md5=$md5_string", $app_secret);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://api-$region.pusher.com/apps/$app_id/events?body_md5=$md5_string&auth_version=1.0&auth_key=$app_key&auth_timestamp=$timestamp&auth_signature=$auth_signature&",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment