Last active
February 26, 2020 06:32
-
-
Save gyohk/4663ebb15286b45b100c6fcdc1995f0c to your computer and use it in GitHub Desktop.
MTのDataAPI使用サンプル(Guzzle使用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Psr\Http\Message\RequestInterface; | |
use \GuzzleHttp\Client; | |
use GuzzleHttp\HandlerStack; | |
use GuzzleHttp\Middleware; | |
date_default_timezone_set('Asia/Tokyo'); | |
$configData = array( | |
'base_url' => 'https://localhost', | |
'username' => 'data_api_user', | |
'password' => 'data_api_password', | |
'clientId' => 'rest_api_name', // MT管理画面で事前に設定する | |
'access_token' => '', | |
'site_id' => '1', | |
'content_data_id' => '3', // コンテンツタイプのID, | |
); | |
$client = new Client(); | |
$sessionId = doLogin($client, $configData); | |
$configData['access_token'] = getToken($client, $configData, $sessionId); | |
$productResource = array( | |
'data' => array( | |
// コンテンツフィールドのIDと値(値の形式はコンテンツフィールドの種類によって異なる) | |
array( | |
'id' => '3', | |
'data' => 'タイトル', | |
), | |
array( | |
'id' => '8', | |
'data' => '本文', | |
) | |
) | |
) | |
registerData($client, $configData, $productResource); | |
exit; | |
// -------------------------------------------- | |
// コンテンツデータを投稿する | |
function registerData($client, $configData){ | |
$url = '/cgi-bin/mt/mt-data-api.cgi/v4/sites/'. $configData['site_id'] .'/contentTypes/' . $configData['content_data_id'] . '/data'; | |
try { | |
$response = $client->request('POST', $url, [ | |
'base_uri' => $configData['base_url'], | |
'allow_redirects' => true, | |
'headers' => [ | |
'X-MT-Authorization' => 'MTAuth accessToken=' . $configData['access_token'], | |
], | |
// 'verify' => false, // 接続先が自己証明書SSLの場合有効にする | |
'form_params' => [ | |
'content_data' => json_encode($productResource), | |
], | |
]); | |
} catch (Exception $e) { | |
$response = $e->getResponse(); | |
echo $response->getBody() . "\n"; | |
return; | |
} | |
$response_body = (string) $response->getBody(); | |
$response_json = json_decode($response_body); | |
return (string) $response_json->id; | |
} | |
// セッションIDを元にアクセストークンを取得する | |
function getToken($client, $configData, $sessionId){ | |
$url = '/cgi-bin/mt/mt-data-api.cgi/v4/token'; | |
try { | |
$response = $client->request('POST', $url, [ | |
'base_uri' => $configData['base_url'], | |
'allow_redirects' => true, | |
'headers' => [ | |
'X-MT-Authorization' => 'MTAuth sessionId=' . $sessionId, | |
], | |
'verify' => false, | |
'on_stats' => function (\GuzzleHttp\TransferStats $stats) use (&$url) { | |
$url = $stats->getEffectiveUri(); | |
}, | |
]); | |
} catch (Exception $e) { | |
$response = $e->getResponse(); | |
echo $response->getBody() . "\n"; | |
exit; | |
} | |
$response_body = (string) $response->getBody(); | |
$response_json = json_decode($response_body); | |
return (string) $response_json->accessToken; | |
} | |
// 管理画面にログインして、セッションIDを取得する | |
function doLogin($client, $configData){ | |
$url = '/cgi-bin/mt/mt-data-api.cgi/v4/authentication'; | |
// エラー制御用 | |
$stack = HandlerStack::create(); | |
$stack->push(Middleware::mapRequest(function (RequestInterface $request) use (&$contentsRequest) { | |
$contentsRequest = (string) $request->getBody(); | |
var_dump($contentsRequest); | |
return $request; | |
})); | |
try { | |
$response = $client->request('POST', $url, [ | |
'base_uri' => $configData['base_url'], | |
'allow_redirects' => true, | |
'verify' => false, | |
'form_params' => [ | |
'username' => $configData['username'], | |
'password' => $configData['password'], | |
'clientId' => $configData['clientId'], | |
'remember' => '1' | |
], | |
]); | |
} catch (Exception $e) { | |
$response = $e->getResponse(); | |
echo $response->getBody() . "\n"; | |
exit; | |
} | |
$response_body = (string) $response->getBody(); | |
$response_json = json_decode($response_body); | |
return $response_json->sessionId; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment