読者です 読者をやめる 読者になる 読者になる

ゆたんぶろぐ

気が向いたら書いてます

Path API を叩く PHP / Python / Ruby のサンプル

API Python php Ruby

あけましておめでとうございます(遅

Path が API を公開してたので(気づくの遅い)、叩いてみました。
今までいくつかの API を叩いてきたけど、json で渡すのは初めてでした。

それぞれ下記を叩くことにします。
GET: /user/:id
https://path.com/developers/docs#get-user

POST: /moment/thought
https://path.com/developers/docs#post-moment-thought

PHP

GET

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

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://partner.path.com/1/user/self');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer 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');

$options = array('thought' => 'PHP Test', 'private' => 1);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://partner.path.com/1/moment/thought');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer YOUR API TOKEN', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($options));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);
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://partner.path.com/1/user/self')
c.setopt(pycurl.HTTPHEADER, ['Authorization: Bearer YOUR API TOKEN'])
c.perform()

POST

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import pycurl
import json

options = {'thought': 'Python test', 'private': 1}

c = pycurl.Curl()
c.setopt(pycurl.URL, 'https://partner.path.com/1/moment/thought')
c.setopt(pycurl.HTTPHEADER, ['Authorization: Bearer YOUR API TOKEN', 'Content-Type: application/json'])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, json.dumps(options))
c.perform()

Ruby

GET

#!/usr/local/bin/ruby
# encoding utf-8
print "Content-Type: text/html\n\n";

require 'faraday'
require 'json'

conn = Faraday::Connection.new(url: 'https://partner.path.com') do |builder|
  builder.use Faraday::Request::UrlEncoded
   builder.use Faraday::Response::Logger
  builder.use Faraday::Adapter::NetHttp
end

response = conn.get do |request|
  request.url '/1/user/self'
  request.headers = {'Authorization' => 'Bearer YOUR API TOKEN'}
end

json = JSON.parser.new(response.body)
p json.parse

POST

#!/usr/local/bin/ruby
# encoding utf-8
print "Content-Type: application/json\n\n";

require 'faraday'
require 'json'

options = {'thought' => 'Ruby Test', 'private' => 1}

conn = Faraday::Connection.new(url: 'https://partner.path.com') do |builder|
  builder.use Faraday::Request::UrlEncoded
  builder.use Faraday::Response::Logger
  builder.use Faraday::Adapter::NetHttp
end

response = conn.post do |request|
  request.url '/1/moment/thought'
  request.headers = {
    'Authorization' => 'Bearer YOUR API TOKEN',
    'Content-Type' => 'application/json'
  }
  request.body = JSON.generate(options)
end

json = JSON.parser.new(response.body)
p json.parse