Skip to content

Instantly share code, notes, and snippets.

@Shaked
Last active April 15, 2018 20:47
Show Gist options
  • Save Shaked/b419b424fd8d1a761b15642d9bfa9ed1 to your computer and use it in GitHub Desktop.
Save Shaked/b419b424fd8d1a761b15642d9bfa9ed1 to your computer and use it in GitHub Desktop.
WIP integration example

WIP integration example

Setup

  1. $ composer install
  2. Change $wipApiKey (see code)
  3. $ php command.php;
<?php
require 'vendor/autoload.php';
//get user id by scraping https://wip.chat
function getWipUserId($username, $useCache = true) {
$scrapingUrl = 'https://wip.chat/@' . $username;
$cacheKey = $username;
$cacheFile = '/tmp/cache_wip_' . $cacheKey;
if ($useCache && !file_exists($cacheFile)) {
$data = file_get_contents($scrapingUrl);
if (preg_match('/wip\.imgix\.net\/store\/user\/(\d+)/', $data, $match) && isset($match[1])) {
$userId = $match[1];
file_put_contents($cacheFile, $userId);
} else {
throw new \Exception('Could not find user id');
}
} else {
$userId = file_get_contents($cacheFile);
}
return $userId;
}
//see https://wip.chat/api
$wipApiKey = 'key';
$wipChatUrl = 'https://wip.chat';
$username = 'shakedko';
$userId = getWipUserId($username);
$payload = sprintf('{
user(id:%d) {
username
todos(completed: true, limit: 1) {
completed_at
}
}
}', $userId);
$client = new \GuzzleHttp\Client(['base_uri' => $wipChatUrl]);
$response = $client->request(
'POST',
'/graphql',
[
// 'debug' => true,
'allow_redirects' => false,
'headers' =>
[
'Authorization' => sprintf('Bearer %s', $wipApiKey),
'Content-Type' => 'application/json',
],
'body' => json_encode(['query' => $payload]),
]
)->getBody()->getContents();
// $response = '{"data":{"user":{"username":"shakedko","todos":[{"completed_at":"2018-03-12T15:22:39Z"}]}}}';
$userData = json_decode($response, true);
if (json_last_error()) {
throw new \Exception(json_last_error_msg());
}
var_dump($userData);
{
"require": {
"guzzlehttp/guzzle": "6.2.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment