Skip to content

Instantly share code, notes, and snippets.

@ykws
Created October 27, 2017 00:17
Show Gist options
  • Save ykws/fcfc0fd69b2853bb8a7fccfbecec5841 to your computer and use it in GitHub Desktop.
Save ykws/fcfc0fd69b2853bb8a7fccfbecec5841 to your computer and use it in GitHub Desktop.
api-locator2.php
<?php
/**
* [ サンプルで行う事 ]
* - APIを利用するためのURLを取得する
*
* [サンプルを動かすための準備]
* PHPにcurlライブラリが組み込まれている必要があります。
* 参考:http://www.php.net/manual/ja/intro.curl.php
*/
define('API_LOCATOR_URL', 'http://www.pi-pe.co.jp/api/locator');
define('API_HTTPHEADER', array(
'X-SPIRAL-API: locator/apiserver/request',
'Content-Type: application/json; charset=UTF-8',
));
if ($argc != 2) exit("Usage: ${argv[0]} [API_TOKEN]" . PHP_EOL);
$parameters = array();
$parameters['spiral_api_token'] = $argv[1];
$json = json_encode($parameters);
$curl = curl_init(API_LOCATOR_URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST , true);
curl_setopt($curl, CURLOPT_POSTFIELDS , $json);
curl_setopt($curl, CURLOPT_HTTPHEADER , API_HTTPHEADER);
curl_exec($curl);
if (curl_errno($curl)) echo curl_error($curl);
$response = curl_multi_getcontent($curl);
curl_close($curl);
$response_json = json_decode($response , true);
if ($response_json['code'] != 0) exit("${response_json['message']} : ${argv[1]}" . PHP_EOL);
echo "URL===>${response_json['location']}" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment