-
-
Save mnaczenski/4cc684ecaaeeaf773ddde07f28a0da2c to your computer and use it in GitHub Desktop.
Shopware Script to delete Articles from a CSV
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
<?php | |
class ApiClient | |
{ | |
const METHOD_GET = 'GET'; | |
const METHOD_PUT = 'PUT'; | |
const METHOD_POST = 'POST'; | |
const METHOD_DELETE = 'DELETE'; | |
protected $validMethods = [ | |
self::METHOD_GET, | |
self::METHOD_PUT, | |
self::METHOD_POST, | |
self::METHOD_DELETE, | |
]; | |
protected $apiUrl; | |
protected $cURL; | |
public function __construct($apiUrl, $username, $apiKey) | |
{ | |
$this->apiUrl = rtrim($apiUrl, '/') . '/'; | |
//Initializes the cURL instance | |
$this->cURL = curl_init(); | |
curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($this->cURL, CURLOPT_FOLLOWLOCATION, false); | |
curl_setopt($this->cURL, CURLOPT_USERAGENT, 'Shopware ApiClient'); | |
curl_setopt($this->cURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); | |
curl_setopt($this->cURL, CURLOPT_USERPWD, $username . ':' . $apiKey); | |
curl_setopt( | |
$this->cURL, | |
CURLOPT_HTTPHEADER, | |
['Content-Type: application/json; charset=utf-8'] | |
); | |
} | |
public function call($url, $method = self::METHOD_GET, $data = [], $params = []) | |
{ | |
if (!in_array($method, $this->validMethods)) { | |
throw new Exception('Invalid HTTP-Methode: ' . $method); | |
} | |
$queryString = ''; | |
if (!empty($params)) { | |
$queryString = http_build_query($params); | |
} | |
$url = rtrim($url, '?') . '?'; | |
$url = $this->apiUrl . $url . $queryString; | |
$dataString = json_encode($data); | |
curl_setopt($this->cURL, CURLOPT_URL, $url); | |
curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST, $method); | |
curl_setopt($this->cURL, CURLOPT_POSTFIELDS, $dataString); | |
$result = curl_exec($this->cURL); | |
$httpCode = curl_getinfo($this->cURL, CURLINFO_HTTP_CODE); | |
return $this->prepareResponse($result, $httpCode); | |
// For Stress testing | |
// return ['result' => $result, 'httpCode' => $httpCode]; | |
} | |
public function get($url, $params = []) | |
{ | |
return $this->call($url, self::METHOD_GET, [], $params); | |
} | |
public function post($url, $data = [], $params = []) | |
{ | |
return $this->call($url, self::METHOD_POST, $data, $params); | |
} | |
public function put($url, $data = [], $params = []) | |
{ | |
return $this->call($url, self::METHOD_PUT, $data, $params); | |
} | |
public function delete($url, $params = []) | |
{ | |
return $this->call($url, self::METHOD_DELETE, [], $params); | |
} | |
protected function prepareResponse($result, $httpCode) | |
{ | |
echo "<h2>HTTP: $httpCode</h2>"; | |
if (null === $decodedResult = json_decode($result, true)) { | |
$jsonErrors = [ | |
JSON_ERROR_NONE => 'No error occurred', | |
JSON_ERROR_DEPTH => 'The maximum stack depth has been reached', | |
JSON_ERROR_CTRL_CHAR => 'Control character issue, maybe wrong encoded', | |
JSON_ERROR_SYNTAX => 'Syntaxerror', | |
]; | |
echo '<h2>Could not decode json</h2>'; | |
echo 'json_last_error: ' . $jsonErrors[json_last_error()]; | |
echo '<br>Raw:<br>'; | |
echo '<pre>' . print_r($result, true) . '</pre>'; | |
return; | |
} | |
if (!isset($decodedResult['success'])) { | |
echo 'Invalid Response'; | |
return; | |
} | |
if (!$decodedResult['success']) { | |
echo '<h2>No Success</h2>'; | |
echo '<p>' . $decodedResult['message'] . '</p>'; | |
if (array_key_exists('errors', $decodedResult) && is_array($decodedResult['errors'])) { | |
echo '<p>' . join('</p><p>', $decodedResult['errors']) . '</p>'; | |
} | |
return; | |
} | |
echo '<h2>Success</h2>'; | |
return $decodedResult; | |
} | |
} |
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
<?php | |
require __DIR__ . '/ApiClient.php'; | |
ini_set('display_errors', 1); | |
ini_set('allow_url_include', 1); | |
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); | |
//Zugangsdaten hinterlegen | |
$client = new ApiClient('http://meineURL/api/', | |
'demo', | |
'n82C76V0VBPlgGBiuiiEPkTVGeyQoXTnTcTCcdDK'); | |
executeDeteletion(); | |
//CSV als Array einlesen | |
function readCSV() | |
{ | |
//Dateiname hinterlegen | |
$csvFile = file('test.csv'); | |
$data = []; | |
foreach ($csvFile as $line) | |
{ | |
$data[] = str_getcsv($line); | |
} | |
return $data; | |
} | |
//Schleife zum Löschen von Artikeln | |
function executeDeteletion() | |
{ | |
$articles = readCSV(); | |
foreach ($articles as $article) | |
{ | |
if($article[0] != 'ordernumber') | |
{ | |
deleteArticle($article[0]); | |
} | |
} | |
} | |
//API-Call zur Löschung | |
function deleteArticle($article) | |
{ | |
global $client; | |
$url = 'articles/'.getArticleID($article); | |
$client->delete($url); | |
} | |
//Artikelnummer in ID umwandeln | |
function getArticleID($ordernumber) | |
{ | |
global $client; | |
$url = 'articles/'.$ordernumber.'?useNumberAsId=true'; | |
$article = $client->get($url); | |
$articleID = $article['data']['id']; | |
return $articleID; | |
} |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
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
ordernumber | |
SW10001 | |
SW10002 | |
SW10003 | |
SW10004 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment