OneSignal Push Notification in PHP: Simplified Function with Example

A very long time I didn’t post a code here. I recently implemented “Web Push Notification” on a few websites using the very popular OneSignal cross platform service. To reuse the method on multiple sites, I simplified the code that handles message sending in a function. In this post I just wanted to share that for my readers.

function osAddPush($oneSignalConfig)
    if (sizeof($oneSignalConfig)) {  
      $notifTitle = html_entity_decode($oneSignalConfig['title'], ENT_QUOTES, 'UTF-8');
      $notifContent = html_entity_decode($oneSignalConfig['brief'], ENT_QUOTES, 'UTF-8');
      $includedSegments = array('All');      

      $fields = array(
        'app_id' => $oneSignalConfig['app_id'],
        'headings' => array("en" => $notifTitle),
        'included_segments' => $includedSegments,
        'isAnyWeb' => true,
        'url' => $oneSignalConfig['url'],
        'contents' => array("en" => $notifContent)
      $thumbnailUrl = $oneSignalConfig['image_url'];

      if (!empty($thumbnailUrl)) {
          $fields['chrome_web_image'] = $thumbnailUrl;

      $logoUrl = $oneSignalConfig['logo_url'];

      if (!empty($logoUrl)) {
          $fields['chrome_web_icon'] = $logoUrl;

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "");
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
                             'Authorization: Basic ' . $oneSignalConfig['app_rest_api_key']));
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($ch, CURLOPT_HEADER, FALSE);
      curl_setopt($ch, CURLOPT_POST, TRUE);
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

      $response = curl_exec($ch);
      return $response;

    return null;
} // EO_Fn    

Its the function that made my reuse easier.

Now, to make it even easier, let me add an example call on that function:

$oneSignalConfig = array(
    'app_id' => 'YOUR_ONE_SIGNAL_APP_ID', // replace with your app_id
    'app_rest_api_key' => 'YOUR_ONE_SIGNAL_REST_API_KEY', // replace with your app_rest_api_key
    'title' => 'Testing the OneSignal Push',
    'brief' => 'Write your brief or summary content here. This will be shown below the title.',
    'url' => 'CONTENT_URL', // URL of the page/post that you're pushing for
    'image_url' => 'CONTENT_IMAGE_URL',
    'logo_url' => 'LOGO_URL', // logo of the company/website

// now do the call

Replace with your own values and make sure you tested it more than once before announcing it for public.

My next post will be a step by step guideline to integrate OneSignal notifications on any website.

Thanks for reading!

openssl_sign(): supplied key param cannot be coerced into a private key

Hello, just wanted to write something coming back after a long time. Please excuse my absence, life has been very busy with so much other priorities.

This post is for those who are using a PHP-DKIM solution and experiencing this below error –

openssl_sign(): supplied key param cannot be coerced into a private key

This happens when you have inputted a wrong private key input in the openssl_sign(). The third parameter is actually the “private key id” received from a call on openssl_get_privatekey(). But often its mistakenly gets passed with a string value of the private key lines.

So, here is the solution –
1) Prepare the “private key id”:
$fp = fopen("/path/to/file/.htkeyprivate", "r");
$privKey = fread($fp, 8192);
$pKeyId = openssl_get_privatekey($privKey, 'optional_passphrase');

2) Use in the openssl_sign:
openssl_sign($dataToSign, $signatureVar, $pKeyId);

If you’re using PHP-DKIM class based solution (object oriented), you may put the below code in the _construct() of the main class:

public function __construct()
$fp = fopen("/path/to/file/.htkeyprivate", "r");
$privKey = fread($fp, 8192);
$pKeyId = openssl_get_privatekey($privKey, 'optional_passphase');

$this->open_SSL_priv = $pKeyId;

Hope this helps you. I will definitely try to write again whenever I get time. Theres a lot to share from regular development experiences but time has bound me.

Happy Blogging!

Some helpful Firefox add-ons for web developers

I want to share some helpful Firefox add-ons that you might like. In my case, they all help me a lot in my development.

Helpful Firefox add-ons:

Firebug: Firebug integrates with Firefox to put a wealth of development tools at your fingertips while you browse. You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page.

See more info:

Firepicker: Simple color picker plugin for Firebug.

See more info:

Live HTTP Headers: View HTTP headers of a page and while browsing.

See more info:

Quick Java: Allows quick enable and disable of Java and Javascript from the status bar.

See more info:

SEO Live YSRank: Live search rank at the status bar, top searched keywords and search volume data for a specific site. Great for search marketing, keywords research and SEO.

See more info:

IE Tab: This is a great tool for web developers, since you can easily see how your web page displayed in IE with just one click and then switch back to Firefox.

See more info:

Validator: This addon for Firebug will send the current HTML source to and display any errors that are returned by this service.

See more info:

View Source Chart: This add-on:
* Graphically Displays HTML Tag Boundaries
* Graphically Defines Tag Nesting Order, Structure and Hierarchy
* Adds Simple and Powerful UI *to* Source Code

See more info:

CSS Reloader: CSS Reloader is an extension that allows you to reload all the CSS of any site without you have to reload the page itself. You can use the keyboard shortcut F9, or the context-menu’s option “Reload CSS” to reload the CSS files or at page.

Very useful especially when you do pixel pushing.

See more info:

FireFTP: FireFTP is a free, secure, cross-platform FTP client for Mozilla Firefox which provides easy and intuitive access to FTP servers.

See more info:

InfoRSS: Displays RSS, Atom, parsed HTML and NNTP feed in a scrolling area in the status bar. Compatible with podcasting RSS which can be downloaded directly. It’s also a good Google email notifier.

See more info:

Link Checker: Check the validity of links on any webpage.

See more info:

RSS Validator: Validates a page using the W3C RSS Validator.

See more info:

Hope this helps. I will share more in future.


Set your own timezone on a shared hosting

If you are a web application developer and had opportunities to work on various client servers around the globe, you may have already experienced the timezone related problems in the server script. This problem comes when the hosting is a shared one and the website runs for a different timezone than the server’s timezone. For example – I am running a website for my local community here in Dhaka/Bangladesh (timezone GMT + 7.00) but my server which is a shared one resides in the United States (say a timezone like GMT – 7). As this is a shared server, the timezone was set by the administrator to their local time. So every call to PHP date() and/or time() function will always return the server’s local time causing a mismatch with my local time in my website.

To overcome this situation, you have to change your default date time using PHP function date_default_timezone_set(your_time_zone_string). For my case, it was following:


Add this line before executing/calling any date and/or time functions.

Below might be a quick test for you:

echo 'OLD Time: ' . date('Y-m-d H:i:s a');


echo '<br />';

echo 'New Time: ' . date('Y-m-d H:i:s a');

That’s it.

Read more on function date_default_timezone_set:

See the list of supported timezones here:

Happy PHPing!

Avoid being blacklisted for SPAM you did not send

If you ever tried to send e-mail messages that never seem to reach the destination, don’t blame PHP limited built-in mail capabilities! You may be victim of having your mail server blacklisted for SPAM that you never sent.

Manuel Lemos of PHPClasses.Org shares an article that guides through the way to avoid being blacklisted for SPAM messages that you never sent. Here is the key points of his article:

  • SPAM traps
  • Auto-replying to spam trap messages
  • Avoiding sending messages to spam trap addresses
  • Using SPF to discard spam trap messages

Read the full article.

I just wanted to share because I found the article interesting.



Google Base Data API Error – You must specify an id for this item

For those who have recently faced this error during product uploading to Google Base, please note that you have to add an additional attribute <g:id> with each of your entries. Google has recently added more documentation on this:



Beendobox :: Check and Send email without leaving Facebook!

Finally we were able to release an Alpha version of the Beendobox on Facebook. Now we can say there is a web application that makes life easier when accessing multiple email accounts. Currently it supports Hotmail, Yahoo & Gmail and hoping AOL & POP3 very soon.

Find it here:

Check and Send email without leaving Facebook! Beendo‘s Beendobox (Alpha) application provides one-stop access to all your email inboxes from Hotmail, Yahoo! Mail, and Google Gmail.

Beendobox on Facebook

Try it out today. Beendobox is safe and secure. Your passwords are not stored unless your choose to “remember passwords” for extra login convenience.

Add this application if you want to make your emailing life easier!

Please send feedbacks (bugs & suggestions) to us so we can work for a better release.