Send push notification to iPhone with PHP and

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 application. This application is made to talk with friends trought web sites and iPhones. 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 widget, pushmeTo sends alerts to my iPhone.

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

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: 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 = "";
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!

Anything to say?