メモ: WebAPIを使ってみる

## サンプル

* https://ja.apis.support.brightcove.com/oauth/guides/getting-access-tokens.html

## 参考

* [jQuery： JSON形式のWeb APIにアクセスするには？（$.getJSON） - Build Insider](http://www.buildinsider.net/web/jqueryref/034)
* [jQuery.getJSON()で別ドメインのJSONPなデータを読み込む時の注意 - kanonjiの日記](http://d.hatena.ne.jp/kanonji/20100427/1272372721)
* [jQuery.getJSON() | jQuery API Documentation](http://api.jquery.com/jquery.getjson/)
* [jQuery-template](http://codepb.github.io/jquery-template/)

## jQuery

![スクリーンショット_2017-02-25_12-19-32.png](https://qiita-image-store.s3.amazonaws.com/0/25728/26383bbf-fbd1-2f78-4148-aae57a21e282.png)


```html:search_address.html
郵便番号
<input id="zip" type="text" size="10" maxlength=7 onKeyUp='search_zip();' />
<input id="search" type="button" value="検索" />

<div id="responses"></div>

<script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
function search_zip() {
    if ($("#zip").val().length != 7) {
	    return false;
    }

    $.getJSON('http://zipcloud.ibsnet.co.jp/api/search?callback=?', {
        zipcode: $('#zip').val()
    })

    .done(function(data) {

      if (data.results) {
        var result = data.results[0];
        $('#responses').html(
            result.address1 + result.address2 + result.address3
        );
      } else {
        $('#responses').html('該当する住所が存在しません。');
      }

    });
}

</script>
```

## jQuery-template

* [jquery.loadTemplate.min.js](https://raw.githubusercontent.com/codepb/jquery-template/master/dist/jquery.loadTemplate.min.js) をダウンロード

```html:_address.html
<span data-content="address1"></span>
<span data-content="address2"></span>
<span data-content="address3"></span>
```

```html:index.html
郵便番号
<input id="zip" type="text" size="10" maxlength=7 onChange='$("#search").click();' />
<input id="search" type="button" value="検索" />

<div id="area_address"></div>

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="jquery.loadTemplate.min.js"></script>
<script>
$(function() {
  $('#search').click(function() {

    $.getJSON('http://zipcloud.ibsnet.co.jp/api/search?callback=?', {
        zipcode: $('#zip').val()
    })

    .done(function(json) {

    $("#area_address").loadTemplate(
      "_address.html",
      {
        "address1" : json.results[0].address1,
        "address2" : json.results[0].address2,
        "address3" : json.results[0].address3,
    });

    });

  });
});
</script>
```

### メモ: curl

* [Developer Resources | Create cool applications that integrate with WordPress.com](https://developer.wordpress.com/docs/api/1.1/get/me/)

```sh
# GET
curl -H GET 'http://localhost:3000/XXXX/XXXX?name=hello&id=100' \
  -H 'Content-Type:application/json;charset=utf-8' \
  -H 'Authorization: Bearer XXXX' \
  | jq .

# POST
curl -H POST 'http://localhost:3000/XXXX/XXXX' \
  -H 'Content-Type: application/json;charset=utf-8' \
  -d '{"name":"hello", "id":"100"}' \
  -H 'Authorization: Bearer XXXX' \
  | jq .
```

### メモ: php

```php
<?php
$options  = array (
  'http' => 
  array (
    'ignore_errors' => true,
    'header' => 
    array (
      0 => 'authorization: Bearer YOUR_API_TOKEN',
    ),
  ),
);
 
$context  = stream_context_create( $options );
$response = file_get_contents(
    'https://public-api.wordpress.com/rest/v1/me/',
    false,
    $context
);
$response = json_decode( $response );

echo $response->message;
```



### メモ: python


```py
import json
import requests

url = 'http://hoge.com'

data = {
        'hoge': 'fuga',
        'fuga' : {'fuga1': 'いいね', 'fuga2': 'だめよ'}
        }

headers = {'Content-Type': 'application/json'}
requests.post(url,data=json.dumps(data), headers=headers)
```

### メモ: ruby

```rb
require 'net/http'

Net::HTTP.get_print 'hoge.com', '/some/param'
```