Created
March 12, 2015 03:24
-
-
Save nakamura-akifumi/c298201d0701c5afb421 to your computer and use it in GitHub Desktop.
自分のIPアドレスをslackに投げるphpスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$incomingUrl = ''; // 発行されたURL | |
$payload = array( | |
'text' => GetMyHostname().'のアドレス:'.GetMyIpAddr(), | |
'username' => 'tmpz84', | |
// 'icon_url' => 'https://slack.global.ssl.fastly.net/12078/img/services/incoming-webhook_48.png', | |
'icon_emoji' => ':beginner:', | |
'channel' => '#server_status', | |
); | |
$result = slackIncomingWebhook($incomingUrl, $payload); | |
var_dump($result); | |
function GetMyHostname() | |
{ | |
$ip = trim(`/bin/hostname`); | |
$ip = preg_replace('/[\n\r\t]/', ',', $ip); | |
return $ip; | |
} | |
function GetMyIpAddr() | |
{ | |
$ip = trim(`/sbin/ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`); | |
$ip = preg_replace('/[\n\r\t]/', ',', $ip); | |
return $ip; | |
} | |
function slackIncomingWebhook($url, $payload) | |
{ | |
$headers = array(); | |
$params = array('payload' => json_encode($payload)); | |
$result = postRequest($url, $params, $headers); | |
return $result; | |
} | |
function postRequest($url, $params, $headers = array()) | |
{ | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($ch); | |
$error = curl_error($ch); | |
curl_close($ch); | |
if ($error) { | |
throw new \Exception($error); | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment