Skip to content

Instantly share code, notes, and snippets.

@einzige
Last active August 10, 2021 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einzige/b1084b077511a8ec916419b648d94c98 to your computer and use it in GitHub Desktop.
Save einzige/b1084b077511a8ec916419b648d94c98 to your computer and use it in GitHub Desktop.
SubscribeStar API PHP example
<?php
/*
* Use the script at your own risk.
* You need to install PHP client first. Tested version 4.7.
* Running the script (bash / shell): CLIENT_ID=**** SECRET=**** REDIRECT_URI=**** SCOPE=**** php api_sample.php
*/
// Store your ENV variables in .env file and use dotenv to run this script or pass them directly as shown above.
define('CLIENT_ID', getenv('CLIENT_ID'));
define('SECRET', getenv('SECRET'));
define('REDIRECT_URI', getenv('REDIRECT_URI')); // Must be URL-encoded eg "https%3A%2F%2Flocalhost%3A3000%2F"
define('REQUEST_SCOPE', getenv('REQUEST_SCOPE') ? getenv('REQUEST_SCOPE') : "subscriber.read+subscriber.payments.read+user.read+user.email.read"); // Use scope you set up in the profile settings for the current App. Multiple scropes delimeter is "+".
define('API_ENDPOINT', getenv('API_ENDPOINT') ? getenv('API_ENDPOINT') : "https://www.subscribestar.com/api/graphql/v1");
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Returns parsed JSON hash or dies on response error
function sendPostRequest($url, array $post = NULL, array $options = array())
{
$curl = curl_init();
$curlDefaults = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
);
if ($post)
{
$curlDefaults[CURLOPT_POSTFIELDS] = http_build_query($post);
}
curl_setopt_array($curl, $curlDefaults + $options);
try
{
$response = curl_exec($curl);
if (empty($response))
{
die(curl_error($curl));
}
else
{
$info = curl_getinfo($curl);
if ($info['http_code'] == "200")
{
echo "Successfully received authorization token.\n\n";
}
else if (empty($info['http_code']))
{
die("No HTTP code was returned\n\n");
}
else
{
die("The server responded: {$info['http_code']}, {$response}\n\n");
}
}
}
finally
{
curl_close($curl);
}
echo "Received Response: {$response}\n\n";
return json_decode($response);
}
// The Code string is used to request a new Token for making API requests.
function requestCode()
{
$clientId = CLIENT_ID;
$secret = SECRET;
$redirectUri = REDIRECT_URI;
$scope = REQUEST_SCOPE;
$url = "https://www.subscribestar.com/oauth2/authorize?client_id={$clientId}&redirect_uri={$redirectUri}&response_type=code&scope={$scope}";
echo ("Please navigate to {$url}\n\n");
echo ("Then enter the enter the code received from authorization URL: ");
$code = preg_replace('/.*code=/', '', trim(fgets(STDIN)));
echo ("\n\n");
return $code;
}
// Requests token used for actual API calls. You exchange Code string with the Token.
// POST https://www.subscribestar.com/oauth2/token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&code=RETURNED_CODE&grant_type=authorization_code&redirect_uri=YOUR_REDIRECT_URL
function requestToken($code)
{
echo ("Obtaining request token using the code: {$code}\n\n");
$clientId = CLIENT_ID;
$secret = SECRET;
$redirectUri = REDIRECT_URI;
$url = "https://www.subscribestar.com/oauth2/token?client_id={$clientId}&client_secret={$secret}&redirect_uri={$redirectUri}&code={$code}&grant_type=authorization_code";
$response = sendPostRequest($url);
$accessToken = $response->{'access_token'};
echo ("Received Access Token: {$accessToken}\n\n");
return $accessToken;
}
$code = requestCode();
$accessToken = requestToken($code);
/*
The simpliest query would be: $query = "{ subscriber { name } }";
To check the subscription status and providing access on your website
you will be likely using price, active, last_time_charged_at fields.
*/
$query = "{
subscriber {
subscription {
price
active
billing_failed
billing_failed_at
cancelled
cancelled_at
last_time_charged_at
}
}
}";
$queryParams = array(
'query' => $query
); // here you can also pass in "variables" parameter.
$response = sendPostRequest(API_ENDPOINT, $queryParams, array(
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer {$accessToken}"
) ,
));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment