Emran Hasan »
25 March 2009 »
In General, PHP, Programming »
Today I’ve written this simple PHP script to alert me through Twitter whenever our company’s local server is down. The script is called by a cron every 5 mins in my central hosting. Without much babble, here goes the code (if you’re interested to know why I needed this, that’s at the bottom of the post):
<?php
// Specify the target URL in your server
$targetUrl = 'http://YOUR_SERVER_URL';
// Specify what the response is from the server
$targetText = 'Hello from Daredevil';
// We will be using cURL for fetching the content
$ch = curl_init();
// Set the params
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Get the response
$response = curl_exec($ch);
curl_close($ch);
// Are things in right place ?
if ($response == $targetText) {
die('Site is up and running!');
}
// Nope, so here are the sender's twitter info
$username = 'SENDER_TWITTER_USERNAME';
$password = 'SENDER_TWITTER_PASSWORD';
// Receiver's twitter username
$receiver = 'RECEIVER_TWITTER_USERNAME';
// Alert message to send
$message = 'Daredevil is not responding, please fix ASAP!';
// The Twitter API address (new direct message)
$url = 'http://twitter.com/direct_messages/new.json';
// We will be using cURL for this
$ch = curl_init();
// Set the params
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$receiver&text=$message");
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
// Send the request
$response = curl_exec($ch);
curl_close($ch);
// Success or failure
if (!empty($response)) {
echo 'Recipient has been notified.';
} else {
echo 'No response from twitter.';
}
Why I needed this?
Recently we have setup a server at our office for committing work to a local SVN repository and have the QA test our work whenever they are ready. We also have a staging server where we do SVN update from this repo. Now, for the last few days, I’ve found the local server to be off due to a few reasons – but every time I realized this at night when I am back home and can’t do anything to turn it on. So I thought about this Twitter alert which is sent to my cell phone immediately when the server goes offline.
Btw, if Twitter doesn’t send SMS to your country, don’t worry. Check out the excellent service at Twe2 that I’ve been using for a couple days.
Cheers!
Continue reading...
Tags: api, application, curl, PHP, quick, REST, tips, twitter
Emran Hasan »
18 February 2009 »
In Code Igniter, General, PHP, Programming, Web Development »
Greetings to all the readers of my blog.
Many of you have been writing to me in the last couple days when I took the site down. The main objective was to add the new theme and push a few code updates of the existing libraries. I really appreciate your concern and would like to reassure you that the site is up and will be up as usual
Now, besides the slightly customized theme from Elegant Themes, I have put a few code updates. They are detailed below:
Extended Model for CodeIgniter
The original version of the Extended Model for CodeIgniter has been serving many people well. Although most users loved the nifty functions of the Model, many (including me) didn’t like the hacking of CI core to get this functionality. With the release of CodeIgniter 1.7, we can avoid that as we can now overload the Model class of CI like the other libraries. Follow this instruction:
1. Download the updated version from here
2. Put it in your application/libraries folder
3. In your model files, use it this way: class Product extends MY_Model
4. Everything else is same just like the original one
HTTP Class
There is not much update in this class except for a few bug fixes (thanks to you guys). Also, I have included a license file in the package as many of you have asked. It’s released under the MIT license. The original post is here for reference.
Download the update from here and the API reference is here.
Cross-domain AJAX calls using PHP
A minor bug fix in the code. Thanks to a few of you who pointed them out. Original post is here. Download the update from here.
I have accelerated plans for the blog in 2009 so stay tuned for some worthy posts in this month. And do write to me if you feel you have any questions/ideas/suggestions.
Cheers!
Continue reading...
Tags: ajax, class, Code Igniter, cross-domain, curl, essential, library, PHP, update, web
Emran Hasan »
20 January 2008 »
In PHP, Programming, Web Development »
Feb 2009: A couple bugs have been fixed and library updated.
This is a wrapper HTTP class that uses either cURL or fsockopen to harvest resources from the web. It supports a handy subset of functionalists of HTTP that are mostly needed in day to day coding. Scripts who need to communicate with other servers will find it useful. If you’re looking to invoke any RESTful API and don’t want to bother adding a bunch of libraries for that simple thing, just put this class and you’re set.
Detailed documentation can be found here. And you can download the source from here.
UPDATE: Class added in Orchid – “PHP framwork for the rest of us”
Features
- Can use both cURL and fsockopen.
- Degrades to fsockopen if cURL not enabled.
- Supports HTTP Basic authentication.
- Supports defining custom request headers.
- Supports defining connection timeout values.
- Supports defining user agent and referral values.
- Supports both user-defined and persistent cookies.
- Supports secure connections (HTTPS) with and without cURL.
- Supports adding requests parameters for both GET and POST.
- Supports automatic redirection (maximum redirect can be defined).
- Returns HTTP response headers and response body data separately.
Example 1 – Simple Get (Facebook Application List)
1: <?php
2:
3: include_once('class.http.php');
4:
5: $http = new Http();
6:
7: $http->execute('http://www.facebook.com/apps/index.php?sort=6');
8: echo ($http->error) ? $http->error : $http->result;
9:
10: ?>
Example 2 – Invoking Yahoo Term Extraction API
1: <?php
2:
3: include_once('class.http.php');
4:
5: $http = new Http();
6:
7: $http->addParam('appid' , 'a_really_random_yahoo_app_id');
8: $http->addParam('context' , 'I am happy because I bought a new car');
9: $http->addParam('output' , 'xml');
10:
11: $http->execute('http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction');
12: echo ($http->error) ? $http->error : $http->result;
13:
14: ?>
Example 3 – Logging into Basecamp (without using cURL!)
1: <?php
2:
3: include_once('class.http.php');
4:
5: $http = new Http();
6:
7: $http->useCurl(false);
8: $http->setMethod('POST');
9:
10: $http->addParam('user_name', 'emran');
11: $http->addParam('password', 'hasan');
12:
13: $http->setReferrer('https://someproject.projectpath.com/login');
14: $http->execute('https://someproject.projectpath.com/login/authenticate');
15:
16: echo ($http->error) ? $http->error : $http->result;
17:
18: ?>
Example 4 – Getting a protected feed
1: <?php
2:
3: include_once('class.http.php');
4:
5: $http = new Http();
6: $http->setAuth('emran', 'hasan');
7:
8: $http->execute('http://www.someblog.com/protected/feed.xml');
9: echo ($http->error) ? $http->error : $http->result;
10:
11: ?>
Download
 |
class.http.php HTTP Class for PHP (supports both cURL and fsockopen) Downloaded: 4937 times |
Continue reading...
Tags: class, curl, emran, hasan, http, PHP, REST, web