Skip to content

Instantly share code, notes, and snippets.

@antydemant
Last active February 11, 2020 23:58
Show Gist options
  • Save antydemant/61ab1e35ae9eae6ab5e8d62daec403bf to your computer and use it in GitHub Desktop.
Save antydemant/61ab1e35ae9eae6ab5e8d62daec403bf to your computer and use it in GitHub Desktop.
Akeneo API - Update SKUs
<?php
////////////////////////////////// ˅˅˅ Credentials ˅˅˅ ////////////////////////////////
/*
* Path to the file
*/
define("FILE_PATH", "EXAMPLE.csv");
/*
* API Host
*/
define("API_HOST", "http://localhost:8080");
/*
* Client ID
*/
define("CLIENT_ID", "1_1tfxl2vh5phcwsg088ksw0k0o4ggw8osso0okk0wo0o44o8coc");
/*
* Secret
*/
define("SECRET", "2u0w7nygaqas88cgw48c4sgoo04sgo0wwg44kco8skoc08884g");
/*
* API Username
*/
define("USERNAME", "<>");
/*
* API Password
*/
define("PASSWORD", "4deea4e51");
////////////////////////////////// ^^^ Credentials ^^^ ////////////////////////////////
$file = fopen(FILE_PATH,"r");
$iteration = 1;
$response = akeneoAuth();
$token = $response["access_token"];
while (!feof($file))
{
if ($iteration % 50 == 0) {
// Resend authorization request in case of long requests on each 50 iteration
$response = akeneoAuth();
$token = $response["access_token"];
}
$data = fgetcsv($file);
if (!empty($data[0]) && $data[0] !== "Item Number") {
if(checkIfAkeneoProductExist($token, $data[0])) {
$updatedProduct = akeneoUpdatedProduct($token, $data[0]);
echo '[Response code] = Type:' . gettype($updatedProduct) . ' Value:' . $updatedProduct . PHP_EOL;
if ($updatedProduct === 204) {
echo "SKU:" . $data[0] . " update was successful." . PHP_EOL;
} else {
echo "SKU:" . $data[0] . " update was failed." . PHP_EOL;
}
} else {
echo "SKU:" . $data[0] . " update was skipped." . PHP_EOL;
}
}
$iteration++;
}
fclose($file);
echo "[Job executed]";
exit(0);
function akeneoAuth() {
$authorization_header = base64_encode(CLIENT_ID . ':' . SECRET);
echo "[Authorization header encoded] = " . $authorization_header . PHP_EOL;
$postFields = "{\n \"grant_type\": \"password\",\n \"username\": \"". USERNAME . "\",\n \"password\": \"" . PASSWORD . "\"\n}";
try {
$response = makeRequest("POST", API_HOST . "/api/oauth/v1/token", array(
"Authorization: Basic " . $authorization_header,
"Content-Type: application/json",
), $postFields);
$response = json_decode($response['data'], JSON_FORCE_OBJECT);
if(isset($response["access_token"])) {
echo "[Access token] = " . $response["access_token"] . PHP_EOL;
return $response;
} else {
echo "[Access token error] = Response message: " . print_r($response, true) . PHP_EOL;
exit(1);
}
} catch (Exception $e) {
echo "[Access token error] = Error message: " . $e->getMessage() . "| Line:" . $e->getLine() . PHP_EOL;
exit(1);
}
}
function akeneoUpdatedProduct($token, $productSKU) {
$postFields = "{\n\t\"identifier\": \"" . $productSKU . "\",\n\t\"enabled\": false\n}";
try {
$response = makeRequest("PATCH", API_HOST . "/api/rest/v1/products/" . $productSKU, array(
"Authorization: Bearer $token",
"Content-Type: application/json",
), $postFields);
if ($response['code'] !== 204) {
echo "[SKU #$productSKU Update error] = Response message: " . print_r($response, true) . PHP_EOL;
}
return $response['code'];
} catch (Exception $e) {
echo "[SKU #$productSKU Update error] = Error message: " . $e->getMessage() . "| Line:" . $e->getLine() . PHP_EOL;
return 0;
}
}
function checkIfAkeneoProductExist($token, $productSKU) {
try {
$response = makeRequest("GET", API_HOST . "/api/rest/v1/products?search={\"identifier\":[{\"operator\":\"=\",\"value\":\"$productSKU\"}]}", array(
"Authorization: Bearer $token",
"Content-Type: application/json",
));
$response = json_decode($response['data'], JSON_FORCE_OBJECT);
if (isset($response["_embedded"])) {
if (isset($response["_embedded"]["items"]) && !empty($response["_embedded"]["items"])) {
return true;
} else {
echo "[SKU #$productSKU Check error] SKU was not found" . PHP_EOL;
}
} else {
echo "[SKU #$productSKU Check error] = Response message: " . print_r($response, true) . PHP_EOL;
}
} catch (Exception $e) {
echo "[SKU #$productSKU Check error] = Error message: " . $e->getMessage() . "| Line:" . $e->getLine() . PHP_EOL;
}
return false;
}
function makeRequest($type, $url, $http_header, $post_fields = null) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $type,
CURLOPT_POSTFIELDS => $post_fields,
CURLOPT_HTTPHEADER => $http_header,
));
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
throw new Exception($err);
} else {
return [
'data' => $response,
'code' => $httpcode
];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment