PHP and Python samples that hit the ChatWork API

ChatWork API preview has been released ! Well, I applied for it immediately and tried using it, but it was too easy and I got wet in various ways.

So I'll put a simple sample of hitting a simple ChatWork API.

I will hit the following respectively. GET: /me http://developer.chatwork.com/ja/endpoint_me.html

POST: /rooms/{room_id}/messages http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-messages

PUT: /rooms/{room_id} http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id

PHP GET

php::get.php


<?php
header('Content-type: application/json; charset=utf-8');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.chatwork.com/v1/me');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-ChatWorkToken: YOUR API TOKEN'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);
curl_close($ch);

echo $response;

POST

php::post.php


<?php
header('Content-type: application/json; charset=utf-8');

$option = array('body' => 'PHP Test');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.chatwork.com/v1/rooms/{room_id}/messages');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-ChatWorkToken: YOUR API TOKEN'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($option, '', '&'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);
curl_close($ch);

echo $response;

PUT

php::put.php


<?php
header('Content-type: application/json; charset=utf-8');

$option = array('description' => 'PHP Test Room', 'icon_preset' => 'beer');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.chatwork.com/v1/rooms/{room_id}');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-ChatWorkToken: YOUR API TOKEN'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($option, '', '&'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = count($option) > 0 ? curl_exec($ch) : array();
curl_close($ch);

echo $response;

Python GET

py::get.py


#!/usr/local/bin/python
# -*- coding: utf-8 -*-
print "Content-Type: application/json";

import pycurl

c = pycurl.Curl()
c.setopt(pycurl.URL, 'https://api.chatwork.com/v1/me')
c.setopt(pycurl.HTTPHEADER, ['X-ChatWorkToken: YOUR API TOKEN'])
c.perform()

POST

py::post.py


#!/usr/local/bin/python
# -*- coding: utf-8 -*-
print "Content-Type: application/json";

import pycurl
import urllib

option = {'body': 'Python Test'}

c = pycurl.Curl()
c.setopt(pycurl.URL, 'https://api.chatwork.com/v1/rooms/{room_id}/messages')
c.setopt(pycurl.HTTPHEADER, ['X-ChatWorkToken: YOUR API TOKEN'])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, urllib.urlencode(option))
c.perform()

PUT

py::put.py


#!/usr/local/bin/python
# -*- coding: utf-8 -*-
print "Content-Type: application/json";

import pycurl
import urllib

option = {'description': 'Python test Room', 'icon_preset': 'beer'}

c = pycurl.Curl()
c.setopt(pycurl.URL, 'https://api.chatwork.com/v1/rooms/{room_id}')
c.setopt(pycurl.HTTPHEADER, ['X-ChatWorkToken: YOUR API TOKEN'])
c.setopt(pycurl.CUSTOMREQUEST, 'PUT')
c.setopt(pycurl.POSTFIELDS, urllib.urlencode(option))
c.perform()

Recommended Posts

PHP and Python samples that hit the ChatWork API
Hit the Sesami API in Python
[Python] Hit the Google Translation API
Hit the Etherpad-lite API with Python
Hit the web API in Python
Summary of the differences between PHP and Python
Hit the Firebase Dynamic Links API in Python
Try using ChatWork API and Qiita API in Python
Send and receive Gmail via the Gmail API using Python
Hit Mastodon's API in Python
Zabbix API this and that
Call the API with python3.
Try hitting the Twitter API quickly and easily with Python
An easy way to hit the Amazon Product API in Python
The story of Python and the story of NaN
Getting the arXiv API in Python
How to get followers and followers from python using the Mastodon API
Create REST API that returns the current time with Python3 + Falcon
Cooperation between python module and API
Hit the New Relic API in Python to get the server status
This and that of python properties
Use the Flickr API from Python
Verification of the theory that "Python and Swift are quite similar"
Access the Twitter API in Python
Python beginners hit the unofficial API of Google Play Music to play music
Python patterns that have been released to the world and have been scrutinized later
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Asynchronous API that combines API Gateway and Step Functions was the strongest story
Hit a method of a class instance with the Python Bottle Web API
Get the number of articles accessed and likes with Qiita API + Python
[python] Move files that meet the conditions
Avoid nested loops in PHP and Python
Try using the Wunderlist API in Python
A class that hits the DMM API
Try using the Kraken API in Python
PHP / Python / Ruby sample hitting Path API
Difference between PHP and Python finally and exit
Python3 + pyperclip that rewrites the copied text
Academia Potter and the Mysterious Python Pass
Try hitting the YouTube API in Python
[Python] A program that rounds the score
Python open and io.open are the same
A Python script that reads a SQL file, executes BigQuery and saves the csv
Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)
Note that I understand the least squares algorithm. And I wrote it in Python.
Install the Python API of the autonomous driving simulator LGSVL and execute the sample program
[Python] A program that finds the minimum and maximum values without using methods
[Python] A program that calculates the number of updates of the highest and lowest records
[Free] Hit the Clash Royale API from lambda and send it to LINE
Examples and solutions that the Python version specified in pyenv does not run
Get the last element of the array by splitting the string in Python and PHP
Created a Python wrapper for the Qiita API
The answer of "1/2" is different between python2 and 3
A nice nimporter that connects nim and python
Hit the Web API using requests Example: Flickr
[Xonsh] The Python shell is sharp and god
Try using the BitFlyer Ligntning API in Python
ffmpeg-Build a python environment and split the video
Specifying the range of ruby and python arrays
About the difference between "==" and "is" in python
Send messages to Skype and Chatwork in Python