çµç·¯
æãMovableTypeã®ãã¼ã¿APIãå©ãã¦å¤§éã®ãã¼ã¿ãæ稿ããã¨ããããã£ã¦ããã ãã©ããã®ããã¯XMLRPCã使ã£ã¦ãã
ã¤ãããã¯ãRest APIã§ããï¼ã£ã¦ãã§ãMovableTypeã®ãã¼ã¿API V6ã§ã¯Restã主æµã£ã½ãã£ããå¥ã«XMLRPCã使ãããã ãããã©ã
ã¨ãããã¨ã§ãPHPã®Guzzlehttpã使ã£ã¦ããã ãã©ãèªè¨¼ç³»ã¨ãã¼ã¿ã®åå¾ç³»ã¯è¡ãããã©ããã¼ã¿ã®æ´æ°ç³»ãå
¨ç¶ãã¾ããããã«å°ã£ã¦ãã¾ã£ã¦ã¯ã³ã¯ã³ã¯ã¯ã¼ã³ã
JavaScriptã®DataAPIã®ãµã³ãã«ããããã
DataAPI使ã£ããµã³ãã«ãªãããªã¼ã£ã¦å
¬å¼ã®ãã¼ã¸ãè¦ã¤ãããã ãã©ãèªãã§ããã¼ããããâ¦ã¨ãããããNodeã§ããã®ï¼ãã¼ãã¨ãæã£ã¦ãããããã¼ã¸ããã£ãããã10å¹´åããï¼
knowledge.sakura.ad.jp
ã§ä½ã£ã¦ã¿ã
ãããªæããJavaScriptSDKã¯mt-staticãã©ã«ãã«ããã®ã§ãããã使ããä»åã® mt-staticã®ãã¹ã¯ âã®ã³ã¼ããåç
§ã
https://mt.example.com/test_data_api.html ãä½ã£ã¦ãhttps://mt.example.com/mt.cgi ã®MovableTypeã®ãã¼ã¿APIãå©ãã¤ã¡ã¼ã¸ãMovableTypeã®ç®¡çç»é¢ããAPIã®è¨±å¯ãåºãã¦ããã¦ããã
ãã¨ãEntryãæ稿ããã«ã¯èªè¨¼ãå¿
è¦ãªã®ã§ãèªè¨¼éã£ã¦ãªãå ´åã¯ãLoginãã®ãªã³ã¯ãã¯ãªãã¯ãã¦ID/Webãµã¼ãã¹ãã¹ã¯ã¼ãï¼ç®¡çç»é¢ã¸ã®ãã°ã¤ã³PASSWORDã§ã¯ãªãï¼åããã°ã¤ã³ç»é¢ã使ããã©ãï¼ã§ãã°ã¤ã³ããã
<html>
<head>
<meta charset="UTF-8"/>
<title>MT Data API</title>
<script src="/mt-static/data-api/v6/js/mt-data-api.min.js"></script>
</head>
<body>
<div id="entries"></div>
<p><a href="#" id="login">Login</a></p>
<p>UserName:<span id="username"></span></p>
<button id="createEntry">Create Entry</button>
<script>
const siteId = 1;
var api = new MT.DataAPI({
baseUrl: "/mt-data-api.cgi",
clientId: "your-client-id"
});
function goToAuthorizationUrl() {
const href = location.href;
document.getElementById('login').href = api.getAuthorizationUrl(href);
}
if (! api.getTokenData()) {
goToAuthorizationUrl();
}
api.listEntries(siteId, function(response) {
let html = '';
if (response.error) {
html += "<p>ãã¼ã¿åå¾ã¨ã©ã¼ã§ãã</p>";
return;
}
html += "<ul>";
for (var i = 0; i < response.items.length; i++) {
var entry = response.items[i];
html += "<li>" + entry.title + "</li>";
}
html += "</ul>";
var entries = document.getElementById("entries");
entries.innerHTML = html;
});
api.getUser('me', function(response) {
if (response.error) {
if (response.error.code === 401) {
goToAuthorizationUrl();
return;
}
return;
}
var user = response;
document.getElementById('login').style.display = 'none';
document.getElementById('username').textContent = user.displayName;
});
const entryData = {
title : "entry from api",
body: 'body sample'
};
document.getElementById('createEntry').addEventListener('click', ()=> {
api.createEntry(siteId, entryData, function(response) {
console.log(response);
});
});
</script>
</body>
</html>
POSTãã¼ã¿ã«ã¤ãã¦
ã§ãä¸è¨ã®ã¹ã¯ãªãããåããã¨ãPOSTãã¼ã¿ã以ä¸ã®ããã«ãªã£ã¦ãããï¼HTTPãªã¯ã¨ã¹ãã®ããã£é¨åï¼
------WebKitFormBoundaryfgVhBpmmR6viaXNh
Content-Disposition: form-data; name="entry"
{"title":"entry from api","body":"body sample"}
------WebKitFormBoundaryfgVhBpmmR6viaXNh--
Content-Type: multipart/form-data;
ã§éãã®ã¯ç¥ã£ã¦ããã©ãContent-Disposition: form-data; name="entry"
ããã¯ãªããï¼ããããã°ãMTå´ã§POSTãã¼ã¿ããã§ãã¯ãã¦ããæã«Entryã£ã¦ãã¼ã¿ããªããã£ã¦ã¨ã©ã¼ãåºã¦ããã ããªâ¦ããã§æå®ããã®ãâ¦
ä»ã¾ã§ã®POSTãã¼ã¿
ä»ã¾ã§Guzzlehttpã§
<?php
$response = $client->post($url, [
'headers' => [
'X-MT-Authorization' => 'MTAuth accessToken=' . $access_token,
],
'json' => [
'entry' => [
'title' => 'sample entry title',
'body' => 'sample entry BODY',
'status' => 'Publish',
'date' => '2025-01-17T12:00:00Z',
],
'publish' => 1,
],
]);
ã¨ããã£ã¦ããã©ããããªJSONãè¿ã£ã¦ããã
{"error":{"code":400,"message":"A resource \"entry\" is required."}}
Content-Disposition: form-data; name="entry"
ãå«ã¾ãã¦ãªãããMTå´ã§ãEntryãã¼ã¿ãç¡ããã£ã¦è¨ããã¦ãã®ãâ¦
æ£ããPOSTãã¼ã¿
ãããªæããBodyé¨åã¯multipartã§æå®ããã®ã ã£ãã
<?php
$response = $client->post($url, [
'headers' => [
'X-MT-Authorization' => 'MTAuth accessToken=' . $access_token,
],
'multipart' => [
[
'name' => 'entry',
'contents' => '{"title":"entry from api(PHP)","body":"body sample"}',
],
],
]);