Path a publié une API (lente à remarquer), alors je l'ai frappée. Jusqu'à présent, j'ai rencontré des API, mais c'est la première fois que je la passe avec json.
Je vais frapper le suivant respectivement.
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