Created
August 28, 2017 11:33
-
-
Save martinleblanc/7a72444053bbe4aa83e5b96542ca4e9d to your computer and use it in GitHub Desktop.
Iconfinder API example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------- index.php: | |
<?php | |
include("settings.php"); | |
include("helpers/token.php"); | |
include("helpers/curl.php"); | |
// Get token. | |
$token = json_decode(get_token($CLIENT_ID, $CLIENT_SECRET, $AUTH_URL)); | |
if(sizeof($token->access_token) == 0) { | |
echo "Error: Token not generated correctly."; | |
exit(); | |
} | |
// Set headers. | |
$headers = array(); | |
$headers[] = 'Authorization: JWT ' . $token->access_token; | |
$headers[] = 'Origin: ' . 'http://www.example.com'; | |
$get_parameters = array(); | |
$url = The URL you want to access. | |
$result = curl_get($url, $get_parameters, $headers); | |
$json_result = json_decode($result); | |
?> | |
--------- heplers/curl.php: | |
<?php | |
/** | |
* Send a GET requst using cURL | |
* @param string $url to request | |
* @param array $get values to send | |
* @param array $options for cURL | |
* @return string | |
*/ | |
function curl_get($url, | |
array $get = NULL, | |
array $headers = array()) { | |
$ch = curl_init(); | |
// Set URL. | |
$url = $url . (strpos($url, '?') === FALSE ? '?' : '') . http_build_query($get); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
if(!$result = curl_exec($ch)) { | |
trigger_error(curl_error($ch)); | |
} | |
curl_close($ch); | |
return $result; | |
} | |
?> | |
--------- helpers/token.php: | |
<?php | |
/** | |
* Send a POST requst using cURL | |
* @param string $CLIENT_ID the client ID | |
* @param string $CLIENT_SECRET the client secret | |
* @return string | |
*/ | |
function get_token($CLIENT_ID, $CLIENT_SECRET, $AUTH_URL) { | |
// Request token | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $AUTH_URL); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=jwt_bearer&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET"); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
// Prepare response | |
header('Content-type: application/json'); | |
// return JWT to requesting app | |
if($result == FALSE) { | |
// Something went wrong. | |
echo json_encode(array("error" => "CURL request failed.")); | |
return false; | |
} | |
else { | |
$result_decoded = json_decode($result); | |
if(array_key_exists("error", $result_decoded) or !array_key_exists("access_token", $result_decoded)) { | |
// Use this for additional error handling. | |
return $result; | |
} | |
else { | |
// Print successful response. | |
return $result; | |
} | |
} | |
} | |
?> | |
--------- settings.php: | |
$API_DOMAIN = "https://api.iconfinder.com"; | |
$AUTH_URL = "https://www.iconfinder.com/api/v2/oauth2/token"; | |
$CLIENT_ID = "xyz"; | |
$CLIENT_SECRET = "xyz"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment