1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| <?php
/**
©CEDEMO - 1998-2014
Example of Cedemo Rest API CALL in PHP
*/
#Auth Mode
#how the client_id / client_secret should be transmitted to the API
$authMode = "header"; #header / post
#Client ID
$clientID = '';
#Client Secret
$clientSecret = '';
#END OF CONFIGURATION PART
if(empty($clientID) || empty($clientSecret)){
echo "ClientID / ClientSecret must be set in the file";
exit;
}
#Retrieve the access Token
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cedemo.com/get_token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
#Auth parameter in header
if ($authMode == "header") {
curl_setopt($ch, CURLOPT_USERPWD, $clientID . ':' . $clientSecret);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('grant_type' => 'client_credentials'));
} #Auth parameter in post variables
elseif ($authMode == "post") {
curl_setopt($ch, CURLOPT_POSTFIELDS, array('grant_type' => 'client_credentials', 'client_id' => $clientID, 'client_secret' => $clientSecret));
}
$response = curl_exec($ch);
$resultStatus = curl_getinfo($ch);
curl_close($ch);
if ($resultStatus['http_code'] == 200) {
$dom = new DOMDocument;
$dom->loadXML($response);
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$simpleXML = simplexml_import_dom($dom);
#display the xml object
echo "<pre>";
print_r($simpleXML);
echo "</pre>";
#display the access token
$accessToken = (string)$simpleXML->access_token[0];
echo "<pre>";
var_export($accessToken);
echo "</pre>";
#Perform metadata videogame search request
$aParams = array('title' => 'Fifa 14', 'access_token' => $accessToken);
$queryParams = http_build_query($aParams);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cedemo.com/metadata/videogame/search?" . $queryParams);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$resultStatus = curl_getinfo($ch);
curl_close($ch);
if ($resultStatus['http_code'] == 200) {
$dom = new DOMDocument;
$dom->loadXML($response);
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$simpleXML = simplexml_import_dom($dom);
#display the xml object
echo "<pre>";
print_r($simpleXML);
echo "</pre>";
if (isset($simpleXML->MetaDataRef[0])) {
if (isset($simpleXML->MetaDataRef[0]->attributes()->CedemoMetaDataId)) {
$code = (string)$simpleXML->MetaDataRef[0]->attributes()->CedemoMetaDataId;
$codeType = 'CedemoMetaDataId';
} elseif (isset($simpleXML->MetaDataRef[0]->attributes()->EAN)) {
$code = (string)$simpleXML->MetaDataRef[0]->attributes()->EAN;
$codeType = 'EAN';
} elseif (isset($simpleXML->MetaDataRef[0]->attributes()->UPC)) {
$code = (string)$simpleXML->MetaDataRef[0]->attributes()->UPC;
$codeType = 'UPC';
} else {
echo "No code / code type found";
exit;
}
#Perform metadata videogame get request
$aParams = array('code' => $code, 'code_type' => $codeType, 'access_token' => $accessToken);
$queryParams = http_build_query($aParams);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cedemo.com/metadata/videogame/get?" . $queryParams);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$resultStatus = curl_getinfo($ch);
curl_close($ch);
if ($resultStatus['http_code'] == 200) {
$dom = new DOMDocument;
$dom->loadXML($response);
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$simpleXML = simplexml_import_dom($dom);
#display the xml object
echo "<pre>";
print_r($simpleXML);
echo "</pre>";
}
} else {
echo "Error during the request";
echo "<pre>";
print_r($response);
echo "</pre>";
}
} else {
echo "Error during the request";
echo "<pre>";
print_r($response);
echo "</pre>";
}
} else {
echo "Error during the request";
echo "<pre>";
print_r($response);
echo "</pre>";
} |
Partager