Help us understand the problem. What is going on with this article?

ChatWork API を叩く PHP と Python のサンプル

More than 5 years have passed since last update.

ChatWork API が プレビュー公開 されてますね!
でまぁ早速申請出して使ってみてるわけなんですが、超簡単過ぎて色々濡れちゃいました。

ということで、簡単な ChatWork API を叩く簡単なサンプルを置いときますね。

それぞれ下記を叩くことにします。
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
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
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
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

#!/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

#!/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

#!/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()
wings1685
京都で Flügel って名前でフリーエンジニアしてます。Python と Ruby が好きですが、仕事では PHP なんです。
http://flugel.biz/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした