Skip to content

Instantly share code, notes, and snippets.

@bz0
Last active November 10, 2017 16:37
Show Gist options
  • Save bz0/3f08d1246c7031748e8fbc355cccad30 to your computer and use it in GitHub Desktop.
Save bz0/3f08d1246c7031748e8fbc355cccad30 to your computer and use it in GitHub Desktop.
kintoneクライアント(レコード操作のみ)簡易版 作成中
<?php
class client{
public static $config;
public function method(){
switch(strtoupper(self::$config['method'])){
case 'GET':
$req = new get(self::$config);
break;
case 'POST':
$req = new post(self::$config);
break;
case 'PUT':
$req = new put(self::$config);
break;
case 'DELETE':
$req = new delete(self::$config);
break;
default:
break;
}
return $req;
}
}
class get{
private $config;
public function __construct($config){
$this->config = $config;
}
public function record($content){
$file = "record.json";
$req = new request($this->config, $file);
$res = $req->send($content);
return $res;
}
public function records($content){
$file = "records.json";
$req = new request($this->config, $file);
$res = $req->send($content);
return $res;
}
}
class post{
private $config;
public function __construct($config){
$this->config = $config;
}
public function record($content){
$file = "record.json";
$req = new request($this->config, $file);
$res = $req->send($content);
return $res;
}
public function records($content){
$file = "records.json";
$req = new request($this->config, $file);
$res = $req->send($content);
return $res;
}
}
class request{
const VERSION = 1;
private $url;
private $config;
public function __construct($config, $file){
$this->config = $config;
$this->url = "https://{$this->config['subdomain']}.cybozu.com/k/v" . self::VERSION . "/" . $file;
}
private function header(){
$header = array(
"Host: {$this->config['subdomain']}.cybozu.com:443",
"X-Cybozu-API-Token: {$this->config['token']}",
"Content-Type: application/json",
"X-HTTP-Method-Override: {$this->config['method']}"
);
return $header;
}
private function body($content){
$body = array(
"app" => $this->config['app']
);
if (!empty($content)){
$body = array_merge($body, $content);
}
return $body;
}
public function send($content){
$header = $this->header();
$body = $this->body($content);
echo "<pre>";
print_r($header);
print_r($body);
echo "</pre>";
$context = stream_context_create(array(
"http" => array(
"method" => $this->config['method'],
"ignore_errors" => true,
"header" => implode("\r\n", $header),
"content" => json_encode($body),
)
));
echo $this->url . "<br>";
$res = file_get_contents($this->url, FALSE, $context);
print_r(json_decode($res, true));
return $res;
}
}
//実行用
client::config = array(
"subdomain" => "",
"method" => "GET",
"token" => "",
"app" => 0
);
$content = array(
"query" => ""
);
$client = new client();
$res = $client->method('get')->records($content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment