Send push notification to iPhone with PHP and pushme.to

UPDATE: 2013/11/18 This code no longer works, but you can check a similar function here: Sending push notifications with php…

August 9, 2010

UPDATE: 2013/11/18
This code no longer works, but you can check a similar function here: Sending push notifications with php to Android devices

Push service is a technology that allows you to send alerts/notifications to a mobile device. Blackberry has its own push service, iPhone has its own, and also Android devices have their own push services (you can learn more on Wikipedia page).
Push notifications are like “sms” but they are free if you have a flat service contract on your mobile.
I want to send push to my iPhone, but I’m not able to develop an iPhone application that receive push alerts, and I don’t want to send only email alerts.
Ok, let’s recap: I have an iPhone and I need to send notifications to my mobile from my PHP scripts.
So I’ve looked deeply in the appStore and I’ve found pushme.to application. This application is made to talk with friends trought web sites and iPhones.
Pushme.to is good because after you’ve registered you can go on their widget page and create a widget for your account to put on your web site. Well, in this post, I will not use this widget for my web site (I don’t want users to write me), but I will use their widget with a mini web bot to send alerts directly to my device through a PHP function.
It happens this way: the cron job calls my php script, my script uses a small bot to call pushme.to widget, pushmeTo sends alerts to my iPhone.

So, if you want to send push to your iPhone thorught a PHP script, download the pushme.to app (it’s free) on your iPhone, register an account and get your pushme.to widget. From the widget code extract the url called in the iframe, it’s something like this:


http://pushme.to/q/widget/export/?hash=51ff0b6e3c1ce3a7a7e473198e1b6d9a

Then, use this php bot function in your scripts:

function pushMeTo($widgeturl,$text,$signature) {
	$agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12";
	if (!function_exists("curl_init")) die("pushMeTo needs CURL module, please install CURL on your php.");
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $widgeturl);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
	$page = curl_exec($ch);
	preg_match("/form action=\"(.*?)\"/", $page, $form_action);
	preg_match("/textarea name=\"(.*?)\"/", $page, $message_field);
	preg_match("/input type=\"text\" name=\"(.*?)\"/", $page, $signature_field);
	$ch = curl_init();
	$strpost = $message_field[1].'=' . urlencode($text) . '&'.$signature_field[1].'=' . urlencode($signature);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost );
	curl_setopt($ch, CURLOPT_URL, $form_action[1]);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
	$page = curl_exec($ch);
}

Call the function when you want, for example after a database check:

<?
// ... send an alert if database is down...
$url = "http://pushme.to/q/widget/export/?hash=51ff0b6e3c1ce3a7a7e473198e1b6d9a";
if (!mysql_connect( "domain", "user", "password" )) {
	pushmeTo ($url,"Mysql server 'domain' is down!","Your Bot");
}
?>

This small PHP bot “pushmeto” is included in the Mini Bots Class!

Author

I’m a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I’m the founder of Rockit.it and Dailybest.it and I’m actually the Chief Technical Officer of Better Days web agency.


Comments on “Send push notification to iPhone with PHP and pushme.to”

13 thoughts

  1. JakobD says:

    This functionality is also provided by the app “Prowl”. It provides an official API.
    But nice way for using pushme.to :)

  2. Sumy says:

    An alternative Method in Rails (requires the GEM “httparty”)

    HTTParty.post(“YOUR_URL”, {:body => “message=MESSAGE&signature=NAME”})

  3. Vinny Benson says:

    Is there a way to send messages from my iPhone to my website??

  4. Ertan says:

    i need .net version?

  5. Textual content messaging has develop into an integral part of our day-to-day lives. Nobody can deny the insurmountable quantity of convenience it has allowed us when attending issues of both our personal and business affairs. The flexibility to ship quick messages on the go is what makes text messaging so attractive that most of us merely cannot reside without it. It has truly transcended from a mere luxury to a necessity as evidenced by the heavy inflow of cell phones in the market today. Amongst them, the Apple iPhone might be the one which garnered essentially the most public adulation. It’s because the iPhone is the first cellphone that mixed the powers of both mobile phone and computer.

  6. hi!,I really like your writing very so much! percentage we communicate more about your article on AOL? I require an expert in this house to unravel my problem. Maybe that is you! Having a look ahead to look you. Executive Elite, 18a Greycoat Gardens, Greycoat Street, London, SW1P 2QA, 028 2088 0135

  7. Chris says:

    I would like to make a widget to my website, but I wanna customize that.
    (Changing texts to hungarian language, resizing text-areas…etc).
    How could I do that? iFrame isn’t customizable so I don’t have any idea…
    /Sorry for my bad english, it’s not my native./
    Thanks!

  8. google says:

    Great post, just one request, would you be able to explain the fourth paragraph in greater depth please? Thankyou

  9. Guywithnofriends says:

    Thank you very much for posting this great solution.

    I used it to make a php script that runs every 5 minutes from cron and randomly sends me a text message once per hour on average (if rand(1, 12) == 1), so my workmates think I have friends.

    I also replaced the sound file inside the pushmeto app (l.wav) to the iphone tri-tone sound, so it sounds exactly like an SMS.

  10. Mixness says:

    good information, thanks for this share.

  11. 2called-chaos says:

    Hey thanks for this example. In plain ruby it could look like this (you have to change the hash style if you want to use it with ruby < 1.9):

    require "net/http"
    require "uri"

    def pushmeto user, options = {}
    options = { message: '-no message-', signature: 'System'}.merge(options)

    Net::HTTP.post_form(URI.parse("http://pushme.to/#{user}/"), options)
    end

    pushmeto "exampleuser", message: "This is important!"
    pushmeto "exampleuser", signature: "Dr. Who", message: "Important too!"

Comments are closed

Recommended

PHP bot to get wikipedia definitions

Wikipedia, the collaborative and multilingual encyclopedia project, has a lot of usefull terms defined in its database, you can find…

August 29, 2010

How to use Instagr.am photos on your site

UPDATE: 2013-12-04 I’ve made a method in the Mini Bots PHP Class that lets you retrieve images from instagram without…

August 18, 2011

PHP to post on a Facebook page

Hi, I’ve modified the Mini Bot Class, I’ve fixed the Facebook status update and I’ve implemented the function to post…

July 28, 2010

PHP google images mini bot

UPDATE 2013/12/12: Now, in theĀ  Mini Bot Class there is a version of this function that is still working. You…

March 30, 2010

PHP curl bot to update Facebook status

I’ve found this great mini bot from Alste blog, and I’ve decided to add it to the mini bot class.…

March 1, 2010

Ping pingomatic.com services with PHP

Ping-o-matic is a service that calls (ping) server engines and popular services to notify them that you have new contents…

February 24, 2010