Skip to content

Instantly share code, notes, and snippets.

@watchfulli
Last active October 24, 2022 03:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watchfulli/352956e5c1781781c9dd to your computer and use it in GitHub Desktop.
Save watchfulli/352956e5c1781781c9dd to your computer and use it in GitHub Desktop.
PHP exemple file for the Watchful API
<?php
/**
* @package Watchful Documentation
* @author Watchful
* @authorUrl https://watchful.li
* @copyright (c) 2014-2017, Watchful
*/
//Config
define('API_KEY', 'REPLACE_BY_YOUR_API_KEY');
define('BASE_URL', 'https://app.watchful.li/api/v1');
/**
* You can pass the API KEY in the HTTP Header Api-Key
*/
$ch = curl_init(BASE_URL . '/tags');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Api-Key: ' . API_KEY));
echo curl_exec($ch);
/**
* You can pass API KEY in a GET param
* DEPRECATED only use for DEBUG
*/
$ch = curl_init(BASE_URL . '/sites' . '?api_key=' . API_KEY);
echo curl_exec($ch);
/**
* If you want data in XML
*/
$ch = curl_init(BASE_URL . '/tags' . '?limit=1');
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Api-Key: ' . API_KEY,
'Content-type: application/xml',
'Accept: application/xml'
),
);
curl_setopt_array($ch, ($options));
var_dump(curl_exec($ch));
/**
* Create new tag
*/
$tag = '{
"name": "New Tag",
"type": "info"
}';
$ch = curl_init(BASE_URL . '/tags');
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $tag,
CURLOPT_HTTPHEADER => array(
'Api-Key: ' . API_KEY
)
);
curl_setopt_array($ch, ($options));
var_dump(curl_exec($ch));
/**
* Edit tag
*/
$tagId = 45;
$editTag = '{
"name": "Edited Tag",
"type": "important"
}';
$ch = curl_init(BASE_URL . '/tags/' . $tagId);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => $editTag,
CURLOPT_HTTPHEADER => array(
'Api-Key: ' . API_KEY
)
);
curl_setopt_array($ch, ($options));
var_dump(curl_exec($ch));
/**
* Delete tag
*/
$tagId = 45;
$ch = curl_init(BASE_URL . '/tags/' . $tagId);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_HTTPHEADER => array(
'Api-Key: ' . API_KEY
)
);
curl_setopt_array($ch, ($options));
var_dump(curl_exec($ch));
/**
* If your DEV server don't support to execute HTTPS requests (you get blank result),
* you can disable the SSL verification like below
*/
$ch = curl_init(BASE_URL . '/tags');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Api-Key: ' . API_KEY));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo curl_exec($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment