PHP code example of sydefz / freelancer-oauth2-client
1. Go to this page and download the library: Download sydefz/freelancer-oauth2-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sydefz / freelancer-oauth2-client example snippets
php
Sydefz\OAuth2\Client\Provider\FreelancerIdentity;
use Sydefz\OAuth2\Client\Provider\FreelancerIdentityException;
$provider = new FreelancerIdentity([
'clientId' => '<your-client-id>',
'clientSecret' => '<your-client-secret>',
'redirectUri' => '<your-client-redirect-uri>',
'scopes' => [<scopes-array>], // Optional only needed when retrieve access token
'prompt' => [<prompt-step-array>], // Optional only needed when retrieve access token
'advancedScopes' => [<advanced-scopes-array>], // Optional only needed when retrieve access token
'sandbox' => true, // to play with https://accounts.freelancer-sandbox.com
]);
php
// Check given error
if (isset($_GET['error'])) {
exit($_GET['error']);
} elseif (!isset($_GET['code'])) {
// If we don't have an authorization code then get one
// Fetch the authorization URL from the provider; this returns the
// urlAuthorize option and generates and applies any necessary parameters
$authorizationUrl = $provider->getAuthorizationUrl();
// Redirect the user to the authorization URL.
header('Location: ' . $authorizationUrl);
exit;
} else {
try {
// Try to get an access token using the authorization code grant.
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
// Store this bearer token in your data store for future use
// including these information
// token_type, expires_in, scope, access_token and refresh_token
storeAccessTokenInYourDataStore($provider->accessTokenArray);
// We have an access token, which we may use in authenticated
// requests against the freelancer identity and freelancer API.
echo $accessToken->getToken() . "\n";
echo $accessToken->getRefreshToken() . "\n";
echo $accessToken->getExpires() . "\n";
echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\n";
// Using the access token, we may look up details about the
// resource owner.
$resourceOwner = $provider->getResourceOwner($accessToken);
var_export($resourceOwner);
} catch (FreelancerIdentityException $e) {
// Failed to get the access token or user details.
exit($e->getMessage());
}
}
php
$provider = new FreelancerIdentity();
try {
$tokenArray = getAccessTokenFromYourDataStore();
$provider->setAccessTokenFromArray($tokenArray);
if (!$provider->accessToken->hasExpired()) {
$request = $provider->getAuthenticatedRequest(
'POST',
$provider->apiBaseUri.'/projects/0.1/projects/',
[
"headers" => ["Content-Type" => "application/json"],
"body" => '{
"title": "Build my Super Website!",
"description": "I need this website to make visual basic GUIs",
"currency": { "id": 1 },
"budget": { "minimum": 300 },
"jobs": [ { "id": 7 }, { "id": 3 } ]
}'
]
);
$response = $provider->getResponse($request);
var_export($response);
} else {
// refresh your token
}
} catch (FreelancerIdentityException $e) {
// Failed to get response
exit($e->getMessage());
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.