PHP / Python / Ruby sample hitting Path API

Path has published an API (slow to notice), so I hit it. I've hit some APIs so far, but it's the first time I've passed it in json.

I will hit the following respectively. GET: /user/:id https://path.com/developers/docs#get-user

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

PHP GET

php::get.php


<?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::post.php


<?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

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

POST

py::post.py


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

rb::get.rb


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

rb::post.rb


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

Recommended Posts

PHP / Python / Ruby sample hitting Path API
I tried hitting Mastodon API with Ruby (Faraday) / Python (Pycurl) / PHP (Curl)
Java VS PHP VS Python VS Ruby
About Perl, Python, PHP, Ruby
Dynamic proxy with python, ruby, PHP
My favorite boto3 (Python) API sample
Easy-to-understand demo of Imagemap Message of LINE Messaging API [PHP] [Ruby] [Python]
Google Cloud Vision API sample for python
Realize PHP / Python generator with Golang / Ruby
[Basic grammar] Differences between Ruby / Python / PHP
Try hitting the YouTube API in Python
Python closure sample
I tried using mecab with python2.7, ruby2.3, php7
Tips for hitting the ATND API in Python
Grouping combination in Python / Ruby / PHP / Golang (Go)
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
PHP and Python samples that hit the ChatWork API
Handle prime numbers in Python / Ruby / PHP / Golang (Go)
python, php, ruby How to convert decimal numbers to n-ary numbers
Let's write Python, Ruby, PHP, Java, JavaScript side respectively
How to handle JSON in Ruby, Python, JavaScript, PHP
I tried hitting the API with echonest's python client
[Docker] Tutorial (Python + php)
Ruby, Python and map
Ajax + Python + PostgreSQL sample
Evernote API in Python
OpenCV3 Python API list
Python and Ruby split
C API in Python 3
TensorFlow API memo (Python)
Python --Simple multi-thread sample
A note about hitting the Facebook API with the Python SDK
Overlapping combinations with limits in Python / Ruby / PHP / Golang (Go)
Sample source of Observer pattern realized by Java, PHP, Python
Sample image of Python API server for EC2 (public AMI)
[Beginners are worried] Which is better, Ruby, PHP or Python?
Factorial, permutations, (duplicate) combinations in Python / Ruby / PHP / Golang (Go)
Hello World in various languages [Python / PHP / Java / Perl / Ruby]