Skip to content

Instantly share code, notes, and snippets.

@nosuz
Last active June 7, 2016 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nosuz/b6f3b6741903dd48ad57c9245bf6dc39 to your computer and use it in GitHub Desktop.
Save nosuz/b6f3b6741903dd48ad57c9245bf6dc39 to your computer and use it in GitHub Desktop.
Access WorsPress REST API with OAuth authorization sample.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8"/>
{% if article is empty %}
<title>WordPress: Post New</title>
{% else %}
<title>WordPress: Update Post</title>
{% endif %}
</head>
<body>
<form method="post" action="./wp-post.php">
題名:<input name="title" type="text"/ value="{{ article.title.raw }}"><br/>
内容:<br/>
<textarea name="content">{{ article.content.raw }}</textarea><br/>
カテゴリー:<br/>
{% if article is empty %}
{# new post #}
{% for item in categories %}
<input type="checkbox" name="cat[]" value="{{ item.id }}">{{ item.name }}<br>
{% endfor %}
<input type="submit" value="作成"/>
{% else %}
{# edit post #}
{% for item in categories %}
{% if item.id in article.categories %}
<input type="checkbox" name="cat[]" value="{{ item.id }}" checked="checked">{{ item.name }}<br>
{% else %}
<input type="checkbox" name="cat[]" value="{{ item.id }}">{{ item.name }}<br>
{% endif %}
{% endfor %}
{# set article id #}
<input type="hidden" name="id" value="{{ article.id }}">
<input type="submit" value="更新"/>
{% endif %}
</form>
<!-- DISPLAY JSON RESPONSE -->
<pre>
{{ json }}
</pre>
</body>
</html>
<?php
$use_oauth = TRUE; # or FALSE
$secret_file = "secret.json";
/*
{
"password": "XXXX XXXX XXXX XXXX XXXX",
"site_url": "https://SITE_ADDRESS",
"oauth": {
"client_key": "XXXXXXXXXXXXXXX",
"client_secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"access_key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"access_secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
}
*/
if ($use_oauth) {
# For OAtuh
require_once('HTTP/OAuth/Consumer.php');
} else {
# For Application Password Auth
require_once('HTTP/Request2.php');
}
require_once 'Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('.');
$twig = new Twig_Environment($loader, array(
#'cache' => '/tmp/twig_cache', 'auto_reload' => TRUE
));
# cache dir is created by Twig if not exist
$vars = [];
# get site endpoint and auth keys
$io = fopen($secret_file, "r");
$site_data = json_decode(fread($io, filesize($secret_file)));
fclose($io);
$oauth_secret = $site_data->oauth;
$site_url = $site_data->site_url;
$post_endpoint = $site_url . "/wp-json/wp/v2/posts";
$categories_endpoint = $site_url . "/wp-json/wp/v2/categories";
if ($use_oauth) {
$oauth = new HTTP_OAuth_Consumer(
$oauth_secret->client_key,
$oauth_secret->client_secret,
$oauth_secret->access_key,
$oauth_secret->access_secret
);
# Get categories
# auth is not required to get categories. Just for Authed GET sample
# GET OAuth
$result = $oauth->sendRequest($categories_endpoint, array(), 'GET')->getResponse();
} else {
# GET BASIC
$http_request = new HTTP_Request2($categories_endpoint);
$http_request->setHeader('Content-Type: application/json');
$http_request->setAuth($site_data->user, $site_data->password);
$result = $http_request->send();
}
if (($result->getStatus() / 100) == 2) {
$category = json_decode($result->getBody());
} else {
$category = [];
}
$vars['categories'] = $category;
$vars['json'] = json_encode($category, JSON_PRETTY_PRINT);
if (! empty($_POST)) {
$time = new DateTime("now");
$article["title"] = $_POST["title"];
$article["content"] = $_POST["content"];
$article["status"] = "draft";
$article["categories"] = $_POST['cat'];
if (isset($_POST['id'])) {
$post_endpoint = $post_endpoint . '/' . $_POST['id'];
} else {
$article["slug"] = $time->format("YmdHi");
}
if ($use_oauth) {
# POST OAuth
$http_request = new HTTP_Request2();
$http_request->setHeader("Content-Type: application/json");
$http_request->setBody(json_encode($article));
$consumer_request = new HTTP_OAuth_Consumer_Request();
$consumer_request->accept($http_request);
$oauth->accept($consumer_request);
$result = $oauth->sendRequest($post_endpoint)->getResponse();
} else {
# POST BASIC
$http_request = new HTTP_Request2($post_endpoint);
$http_request->setMethod('POST');
$http_request->setHeader('Content-Type: application/json');
$http_request->setAuth($site_data->user, $site_data->password);
$http_request->setBody(json_encode($article));
$result = $http_request->send();
}
$status = $result->getStatus();
$body = json_decode($result->getBody());
$vars['json'] = json_encode($body, JSON_PRETTY_PRINT);
$vars['article'] = $body;
}
$template = $twig->loadTemplate('wp-post.html');
echo $template->render($vars);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment