LoginSignup
23
22

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-01-27

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 -*-
print "Content-Type: application/json";

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
23
22
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
22