Use HipChat PHP API

Hipchat is good communication platform not just between people but also between machine and apps.

I am using HipChat to monitor deployment in Jenkins. There’s HipChat plugin for Jenkins.

Hipchat Jenkins plugin
Hipchat Jenkins plugin

And I would like to get this deployment result using API.

hipchat log
hipchat log

Here we go.

First we need to install composer if it is not installed on the server.

sudo apt-get update
sudo apt-get install curl php-cli php-mbstring git unzip
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php

After get composer setup file, we can verify the installer. go to https://composer.github.io/pubkeys.html and get key.
Run this command after replace key.

php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

You will see the message like “Installer verified”. Then turn this.

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Next step is get HipChat PHP API and configure composer accordingly.

git clone https://github.com/vanilla/hipchat-api.git
curl -sS https://getcomposer.org/installer | php
php composer.phar install

When you git clone, the git repository has composer.json file and run curl -sS https://getcomposer.org/installer | php  will down load composer.phar file and all sub folders will be created after run php composer.phar install

Now we need to get API Token.  Login to HipChat and go to Profile > API access menu > create new token.

get hipchat api token
get hipchat api token

Finally, we can make example script.

<?php
// The library lives in the HipChat namespace.
use HipChat\v2\HipChatClient;

// Require composer's autoloader.
require_once 'vendor/autoload.php';
  
// Define the cli options.
$client = new HipChatClient();
$client->setAuth('{your api token put here}');

// Start making calls!
$options['max-results']=5;
$options['start-index']=0;
$message = $client->roomsAPI()->getHistory('{room number goes here}',$options);
print_r($message);

I got this chat history. Yay!

hipchat api result
hipchat api result

I put it on my Raspberry Pi LCD display to monitor quickly.

hipchat on my raspberry pi
hipchat on my raspberry pi

Get JSON data using CURL

There’s lots of communication using json, this is useful PHP function for getting JSON data using CURL function.

/*
E.g.  
  $args = '{"tid":"'.$taxonomy_id.'"}';
  $content = curl_request('http:/test.com/test.json', $args, 'POST');
*/
if (!function_exists('get_url_data_curl')) {
  function get_url_data_curl($url, $args='', $httpmethod){
    try{
      if (function_exists('curl_init') && function_exists('curl_setopt')){
      	$ch = curl_init();
      	curl_setopt($ch, CURLOPT_URL, $url);
      	curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type:application/json'));     
      	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      	if(strcmp($httpmethod, "POST") == 0){
      		curl_setopt($ch, CURLOPT_POST, true);
      	}else{
      		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpmethod); 
      	}
      	if(strcmp($httpmethod, "POST") == 0 || strcmp($httpmethod, "PUT") == 0 && !empty($args)){
      			curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
      	}
      	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      	$result = curl_exec($ch);
      
      	curl_close($ch);
      	return $result;
      }
    }catch(Exception $e){
      $error = array(
      	'status' => 'failed',
      	'errorMessage' => $e->getMessage(),
      	'errorCode' => '500'
      );
      return json_encode($error);
    } 
  }
}

How to use is simple.

$args = '{"tid":"'.$taxonomy_id.'"}';
$content = curl_request('http:/test.com/test.json', $args, 'POST');

 

Put $args variable what you want to pass through.

Method can be either GET or POST.

Host a website on Raspberry Pi in 30 mins

I have old Raspberry Pi and I want to use it for personal server to running my task manager software.

Install raspbian on SD card

sudo dd bs=1m if=path_of_your_image.img of=/dev/diskn

And turn on Raspberry and check ip address.

ifconfig

And I get 10.50.101.62

Connect using terminal.

ssh pi@10.50.101.62

Make apt-get up to date.

apt-get update

Install apache2

sudo apt-get install apache2 -y

Install php

sudo apt-get install php5 libapache2-mod-php5 -y

Install mysql

sudo apt-get install mysql-server

Install phpmyadmin

sudo apt-get install phpmyadmin
sudo nano /etc/apache2/apache2.conf
Include /etc/phpmyadmin/apache.conf
sudo /etc/init.d/apache2 restart
Access to http://10.50.101.62/phpmyadmin/

I have downloaded task manager from mytinytodo.net and install on the server.

Boom! Here’s my website.

It took about 40 mins to install and host a website and hosting is free, isn’t it cool?

Simple way for uploading file to Amazon S3

I have one project for uploading video file to Amazon S3 server on PHP environment.

To get started with sample, download sample project.

git clone https://github.com/awslabs/aws-php-sample.git

Go to aws-php-sample folder and run. This will install composer.

curl -sS https://getcomposer.org/installer | php
php composer.phar install

 

Create testing php script.

<?php
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;

$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => 'AKXXXXXXXXXXXXXXXXXXQ',
'secret' => 'twHHXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXh'
],
'debug' => false
]);

$bucket = 'test-bucket';
$key = 'test.mp4';
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => $key,
'SourceFile' => '/home/titus/test/aws-php-sample/test.mp4',
]);

screen-shot-2016-11-10-at-10-34-39-am

Run sample from the url or command:

php test.php

If it is not working, change debug value as true. Then you can see what the problem is.

Simple is beautiful as always.