Skip to content

Instantly share code, notes, and snippets.

@localdisk
Created February 7, 2014 19:51
Show Gist options
  • Save localdisk/8870430 to your computer and use it in GitHub Desktop.
Save localdisk/8870430 to your computer and use it in GitHub Desktop.
typetalk api php sample
<?php
$clientId = 'xxxxxxxxxxxxxxxxxxxx';
$clientSecret = 'xxxxxxxxxxxxxxxxxxxx';
$topicId = 'xxx';
$msg = 'Hello! typetalk! use PHP API!';
$tokenQuery = http_build_query([
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
'scope' => 'topic.post'
]);
$tokenHeaders = [
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($tokenQuery)
];
$tokenContext = stream_context_create([
'http' => [
'method' => 'POST',
'content' => $tokenQuery,
'header' => [
implode("\r\n", $tokenHeaders),
]
]
]);
$ret = file_get_contents('https://typetalk.in/oauth2/access_token', false, $tokenContext);
$accessToken = json_decode($ret, true)['access_token'];
$postQuery = http_build_query([
'message' => $msg
]);
$postHeaders = [
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($postQuery),
'Authorization: Bearer ' . $accessToken
];
$postContext = stream_context_create([
'http' => [
'method' => 'POST',
'content' => $postQuery,
'header' => [
implode("\r\n", $postHeaders),
]
]
]);
file_get_contents("https://typetalk.in/api/v1/topics/{$topicId}", false, $postContext);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment