Skip to content

Commit

Permalink
Merge pull request #36 from ivkos/curl-callback
Browse files Browse the repository at this point in the history
Add a cURL callback method
  • Loading branch information
ivkos committed Feb 4, 2015
2 parents 8bc4c38 + 69fd5ba commit 59661e9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
22 changes: 15 additions & 7 deletions README.md
Expand Up @@ -2,7 +2,7 @@ Pushbullet
==========

## Description
Using this class, you can send push notifications to Android, iOS, Chrome and Firefox running **Pushbullet**. The following types of push notifications can be sent:
Using this class, you can send push notifications to mobile and desktop devices running **Pushbullet**. The following types of push notifications can be sent:
* notes
* links
* addresses
Expand All @@ -13,16 +13,12 @@ For more information, you can refer to these links:
* **Official website**: https://www.pushbullet.com
* **API reference**: https://docs.pushbullet.com
* **Blog**: http://blog.pushbullet.com
* **Android app**: https://play.google.com/store/apps/details?id=com.pushbullet.android
* **iOS app**: https://itunes.apple.com/us/app/pushbullet/id810352052
* **Chrome extension**: https://chrome.google.com/webstore/detail/pushbullet/chlffgpmiacpedhhbkiomidkjlcfhogd
* **Firefox extension**: https://addons.mozilla.org/en-US/firefox/addon/pushbullet/
* **Apps**: https://www.pushbullet.com/apps

## Requirements
* PHP >= 5.2.0
* PHP 5.4.0 or newer
* cURL library for PHP
* Your Pushbullet API key (get it here: https://www.pushbullet.com/account)
* PHP for Windows users: if you are getting SSL certificate errors, see issue #25

## Install

Expand Down Expand Up @@ -53,6 +49,18 @@ try {
// Get your API key here: https://www.pushbullet.com/account
$p = new Pushbullet('YOUR_API_KEY');

// If you get SSL errors while using the library, you may need to point cURL to a CA certificate bundle
$p->addCurlCallback(function ($curl) {
// Get a CA certificate bundle here:
// https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt

curl_setopt($curl, CURLOPT_CAINFO, 'C:/path/to/ca-bundle.crt');

// Alternatively, you can disable SSL certificate verification.
// However, this is a bad idea since it makes communication vulnerable to MITM attacks.
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
});


#### Get methods

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -21,7 +21,7 @@
}
],
"require": {
"php": ">= 5.2.0",
"php": ">= 5.4.0",
"ext-curl": "*"
},
"autoload": {
Expand Down
21 changes: 18 additions & 3 deletions src/Pushbullet.php
Expand Up @@ -2,12 +2,13 @@

/**
* Class Pushbullet
*
* @version 2.7.2
*
* @version 2.8.0
*/
class Pushbullet
{
private $_apiKey;
private $_curlCallback;

const URL_PUSHES = 'https://api.pushbullet.com/v2/pushes';
const URL_DEVICES = 'https://api.pushbullet.com/v2/devices';
Expand Down Expand Up @@ -378,10 +379,19 @@ public function sendSms($fromDeviceIden, $toNumber, $message)
'conversation_iden' => $toNumber,
'message' => $message
));

return $this->_curlRequest(self::URL_EPHEMERALS, 'POST', $data, true, true);
}

/**
* Add a callback function that will be invoked right before executing each cURL request.
*
* @param callable $callback The callback function.
*/
public function addCurlCallback($callback) {
$this->_curlCallback = $callback;
}

/**
* Send a push.
*
Expand Down Expand Up @@ -526,6 +536,11 @@ private function _curlRequest($url, $method, $data = null, $sendAsJSON = true, $
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);

if ($this->_curlCallback !== null) {
$curlCallback = $this->_curlCallback;
$curlCallback($curl);
}

$response = curl_exec($curl);

if ($response === false) {
Expand Down

0 comments on commit 59661e9

Please sign in to comment.