Morsecode As A Service: node.js app with restify and Heroku

Posted on 22. Januar 2015 Comments

I wanted to play around with node.js and REST APIs. Heroku is widely used for deploying node.js app, last but not least because they give you one free instance to test your code and the possibility to use your own domain name (via CNAME).

Until now the code is rather trivial. This for example is the encode function (plaintext->morsecode). It uses the npm module morse to encode the given string in the request parameters and returns it with the plaintext in an array. The requests are handled by restify.
function encode(req, res, next) {
var answer = {}
answer.plaintext = req.params.string.toUpperString();
answer.morsecode = morse.encode(req.params.string);
res.send(answer);
next();
}

This function gets called later here.
server.get('/encode/:string', app.encode);

This starts the server on the port from the Heroku instance.
var port = process.env.PORT || 8080;
server.listen(port, function() {
console.log('%s listening at %s', server.name, server.url);
});

The decode function is equivalent, except that I test if there are only „.“, „-“ and white spaces in the request.

I wrote the documentation with the automatic page generator from GitHub Pages in the repository and so with the following code the user is redirected there when entering the root „/“.

 

function redirectToDocumentation(req,res,next) {
res.send(302, null, {
Location: API_DOKU
});
next();
}

It’s reachable under morsecode-api.de or morsecodeapi.herokuapp.com.

Skript mit cat, unzip, grep und awk für eBays XML Responses

Posted on 7. Januar 2015 Comments

Wenn man bei eBay per API z.B. ein FixedPriceItem hinzufügt bekommt man die folgende Antwort nachdem der Prozess erst Scheduled und dann InProcess ist:

Status is Completed
Downloading fixed price item responses...Done
File downloaded to /tmp/add-fixed-price-item-responses-ABC123.zip
Unzip this file to obtain the fixed price item responses.

Den folgenden Code eine Datei schrieben, dann in /usr/local/bin verschieben und mit chmod +x Ausführrechte geben.

cat `unzip -o $1 | grep inflating |awk '{print $2}'`

Mit diesem Skript bekommt man zum debuggen eine schnelle Ausgabe der XML aus der Konsole bewirken:

debugebayxml /tmp/add-fixed-price-item-responses-ABC123.zip

Using a Perl script because refactoring of a project with Android tools didn’t work

Posted on 20. Dezember 2014 Comments

I wanted to rename my project but I guess since it has a lot of dependencies that caused an error somewhere and I got the error message:

Refactoring
A fatal error occurred while performing the refactoring.
An unexpected exception occurred while creating a change object. See the error log for more details.

So I just the normal refactoring feature of Eclipse which not surprisingly also caused an error. After editing the AndroidManifest package entry, the import of the resources in the sources files didn’t work. It still said

import com.example.oldpackage.R

Only a couple of resource files needed manual editing but the Java files were a problem. What did the trick for me was this one-liner

perl -pi -w -e 's/import com.example.oldpackage.R/import com.example.newpackage.R/g;' `grep -r -l "import com.example.oldpackage.R"`

I realise this is rather quick&dirty (do a backup 😉 ) but it did work for this project. A short explanation:

perl

  • -pi puts the code in a loop (like -n, but sed-style)
  • -w gives you warnings
  • -e is one line of programm and since -pi
  • /g global, for all lines in the file

grep

  • -r recursive
  • -l give out files that matches the following search string

How to change nano syntax highlighting for arbitrary filename extensions

Posted on 17. Dezember 2014 Comments

The text editor nano manages syntax highlighting through .nanorc files. Say, we have files with the ending .phpx, but would like PHP syntax highlighting.

$ sudo nano /usr/share/nano/php.nanorc

Change this line

syntax "php" "\.php[2345s~]?$"

to this regex:

syntax "php" "\.php[x2345s~]?$".

Alternativly, e.g. if you don’t have sudo rights for /usr/share/nano/php.nanorc, you can do the following

$cp /usr/share/nano/php.nanorc ~/.phpx.nanorc

Then enter the following line in ~/.nanorc:

include ~/.phpx.nanorc

Then, change the line as mentioned above and syntax „php“ into syntax „phpx“

Automate selling at LaRedoute #2: Parse order files

Posted on 16. Dezember 2014 Comments

This blog post is part of the series Automize selling at LaRedoute.

  • Part 1: Get new orders
  • Part 2: Parse order files
  • Part 3: Upload response files
  • Part 4: update quantity and price feed

Update 2016: La Redoute is going to stop using CSV and moves everything to SOAP Webservices. Tutorials will follow


In part 1 the files containing orders are downloaded into a folder called OrdersFromLaRedoute. The next script is going to go through that folder, parse the file and insert it into a table.

These are the values the table must have because we’re just going to insert everything that’s in the CSV files.


$dbValues = ['MarketplaceID', 'OrderID', 'StorefrontOrderID', 'OrderDate', 'BuyerEmailAddress', 'BuyerName', 'BuyerPhoneNumber', 'OrderItemCode', 'ItemStatus', 'SKU', 'Title', 'Quantity', 'ItemPrice', 'ItemTax', 'ShippingCharge', 'ShippingTax','ItemFee', 'Currency', 'ShippingOption', 'PaymentInfo', 'ShippingAddressName', 'ShippingAddressFieldOne', 'ShippingAddressFieldTwo', 'ShippingAddressFieldThree', 'ShippingCity', 'ShippingStateOrRegion', 'ShippingPostalCode', 'ShippingCountryCode', 'ShippingPhoneNumber', 'BillingAddressName', 'BillingAddressFieldOne', 'BillingAddressFieldTwo', 'BillingAddressFieldThree', 'BillingCity', 'BillingStateOrRegion', 'BillingPostalCode', 'BillingCountryCode', 'BillingPhoneNumber'];

 

Therefore I created the table more or less like this:

CREATE TABLE `ORDERS-HISTORY` (
`MarketplaceID` int(11) DEFAULT NULL,
`OrderID` varchar(50) DEFAULT NULL,
`StorefrontOrderID` varchar(50) DEFAULT NULL,
`OrderDate` varchar(50) DEFAULT NULL,
`BuyerEmailAddress` varchar(50) DEFAULT NULL,
`BuyerName` varchar(50) DEFAULT NULL,
`BuyerPhoneNumber` varchar(50) DEFAULT NULL,
`OrderItemCode` varchar(50) NOT NULL DEFAULT '',
`ItemStatus` varchar(10) NOT NULL DEFAULT '',
`SKU` int(11) DEFAULT NULL,
`Title` varchar(50) DEFAULT NULL,
`Quantity` int(11) DEFAULT NULL,
`ItemPrice` decimal(8,2) DEFAULT NULL,
`ItemTax` decimal(8,2) DEFAULT NULL,
`ShippingCharge` decimal(8,2) DEFAULT NULL,
`ShippingTax` decimal(8,2) DEFAULT NULL,
`ItemFee` decimal(8,2) DEFAULT NULL,
`Currency` varchar(3) DEFAULT NULL,
`ShippingOption` varchar(10) DEFAULT NULL,
`PaymentInfo` varchar(50) DEFAULT NULL,
`ShippingAddressName` varchar(70) DEFAULT NULL,
`ShippingAddressFieldOne` varchar(70) DEFAULT NULL,
`ShippingAddressFieldTwo` varchar(70) DEFAULT NULL,
`ShippingAddressFieldThree` varchar(70) DEFAULT NULL,
`ShippingCity` varchar(70) DEFAULT NULL,
`ShippingStateOrRegion` varchar(70) DEFAULT NULL,
`ShippingPostalCode` int(11) DEFAULT NULL,
`ShippingCountryCode` varchar(3) DEFAULT NULL,
`ShippingPhoneNumber` varchar(50) DEFAULT NULL,
`BillingAddressName` varchar(70) DEFAULT NULL,
`BillingAddressFieldOne` varchar(70) DEFAULT NULL,
`BillingAddressFieldTwo` varchar(70) DEFAULT NULL,
`BillingAddressFieldThree` varchar(70) DEFAULT NULL,
`BillingCity` varchar(70) DEFAULT NULL,
`BillingStateOrRegion` varchar(70) DEFAULT NULL,
`BillingPostalCode` int(11) DEFAULT NULL,
`BillingCountryCode` varchar(3) DEFAULT NULL,
`BillingPhoneNumber` varchar(50) DEFAULT NULL,
PRIMARY KEY (`OrderID`,`OrderItemCode`,`ItemStatus`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

As you can see, the primary key consists of OrderID, OrderItemCode and ItemStatus. An OrderID is the unique identifier of one order, consisting of possibly many but at least one OrderItemCode. An OrderItemCode is representing an SKU + Quantity. For newly created orders, the ItemStatus will appear as „Created“. Once an item is accepted, LaRedoute will put a file into the ToSupplier folder with exactly the same OrderID, OrderItemCode but the ItemStatus „ToShip“. This will be important in Step 3.

Then, use a CSV library like league/csv and insert it with e.g. medoo.


$dirAsArray = scandir("OrdersFromLaRedoute");

foreach($dirAsArray as $file) {
// parse and INSERT
}

Automate selling at LaRedoute #1: Get new orders

Posted on 16. Dezember 2014 Comments

This blog post is part of the series Automize selling at LaRedoute.


The french marketplace LaRedoute unfortunately doesn’t have a real API, but they do have ways to automize some processes. A lot of smaller marketplaces have this concept as well. You will get credentials for an SFTP server. On this server you will find the folders ToSupplier and FromSupplier, where the „supplier“ (aka you) can up- and download a range files documented by Merchantry in their blog. The processing of the uploaded files can take up to 6 hours, but is sometimes done in only a couple of minutes, so I’m going to assume the worst case of 6 hours in this post.

While programming a couple of scripts I found the following problems:

  • the server is incredibly slow sometimes (better at nights), so sometimes the connections just time out
  • sometimes the listing for the ToSupplier folder times out because there are too many files (according to support…huh?), so they have to be deleted regularly
  • not only the connection to LaRedoute but also the connection to my local MySQL server times out
  • I have to reserve a purchased item once I accepted it on LaRedoute immediately, because it could be sold elsewhere in the 6 hours LaRedoute might take to give me the shipping address

New orders can be found in the ToSupplier folder in tab seperated CSV files (but .txt ending) with the format OrdersYYYY-MM-DD-hh-mm-ss.txt.

Since PHP is the companies main language I will show a couple of scripts which automize downloading and processing those files. The code is of course simplified for better understanding. We’re using SFTP instead of FTP and I found using the phpseclib to be the most usable library.

I will propose the use of 2 Tables in the MySQL database: TEMP-FILENAMES and FILENAMES-HISTORY. Both have a the unique column filename. FILENAMES-HISTORY will contain the name of every file ever processed by the following script, TEMP-FILENAMES is a helper table that will be truncated after every run.

First, we need to establish a connection


$sftp = new Net_SFTP(SFTP_LAREDOUTE_HOST);
if (!$sftp->login(SFTP_LAREDOUTE_USER, SFTP_LAREDOUTE_PASS)) {
exit('Login Failed');
}

Then we change directory. This is a command that usually involves listing the directory changed into, but since this is not a graphical client, the real timeout might come on line below. $nlist will just be null if the listing fails, and I will assume it didn’t work if it takes more than 30 seconds.

$sftp->chdir('/ToSupplier');

$beforetime = time();
$nlist = $sftp->nlist();
$aftertime = time();
if(($aftertime-$beforetime) > 30 ) {
exit('Timeout while Listing directory');
}

The next piece of code is only executed if the listing worked. Every filename that includes the word „Order“ is now inserted into the temporary table:

foreach($nlist as $filename) {
if (strpos($filename, 'Order') !== false) {
$qry = "INSERT INTO `TEMP-FILENAMES`(`filename`) VALUES ('". $filename . "')";
$insert = mysql_query($qry,MYSQLCONNECTION) or print mysql_error();
}
}

You can look at the difference between the filenames in your HISTORY table and the possibly new ones in the temporary table.

$tmpCmpFilenames = array();
$qry = "SELECT `filename` FROM `TEMP-FILENAMES` WHERE `filename` NOT IN (SELECT `filename` FROM `FILENAMES-HISTORY`)";
$select = mysql_query($qry, MYSQLCONNECTION) or print mysql_error();
while ($row = mysql_fetch_assoc($select)) {
$tmpCmpFilenames[] = $row['filename-id'];
}

Now we have all the new files in the array $tmpCmpFilenames. The correct way would be make sure the downloaded files are correct with hashes. Instead we decided to misuse the filesize, since it’s a good indicator something didn’t work properly;) The files not downloaded correctly are deleted from the array. They will appear next time the script is run.

foreach($tmpCmpFilenames as $filename) {
$remotefilesize = $sftp->size($filename);
$sftp->get($filename, 'OrdersFromLaRedoute/' . $filename);
$localfilesize = filesize('OrdersFromLaRedoute/' . $filename);
if ($remotefilesize != $localfilesize) {
unset($tmpCmpFilenames[$filename]);
}
}

We can now insert the filenames into the HISTORY table.

foreach($tmpCmpFilenames as $filename) {
$qry = "INSERT INTO `FILENAMES-HISTORY`(`filename`) VALUES ('". $filename . "')";
$insert = mysql_query($qry, MYSQLCONNECTION) or print mysql_error();
}

Last but not least, the temporary table needs to be truncated for the next run.

$truncate=mysql_query("TRUNCATE TABLE `TEMP-FILENAMES`",MYSQLCONNECTION) or print mysql_error();

The next step is described in part 2 of this series.

Minimal Mensa Plan

Posted on 9. Dezember 2014 Comments

We’ve been doing a lot of website scraping for a university project latel so I decided a little app to scrape the universitys‘ cantine website for the lunch menu.

minimalmensaplan-screenshot

A network connection in the main Activity is not allowed so I’m using a private class for that. Once the doInBackground method is over onPostExecute is automatically called.

Luckily the jSoup library which is used for parsing also brings a way download a website for parsing.

Document doc = Jsoup.connect("http://speiseplan.studierendenwerk-hamburg.de/de/520/2014/0/").get();

There is only one category class and it contains the date. The dish-description class contains what you will later see in the app. The size is needed for a for-loop later on.

date = doc.select(".category").text();
int maxDishes = doc.getElementsByClass("dish-description").size();

With the next piece of code all the dish descriptions are extracted. The original website contains details about the food (made with alcohol, pork or if it’s vegeterian etc). For blending this out I use a regular expression which filters out:

1 non-word character(\W = an opening bracket), then possibly multiple digits (\d) and non-word characters (\W = commas) and then another non-word character (\W = a closing bracket)

String dish = doc.getElementsByClass("dish-description").get(i).text().replaceAll("(\\W[\\d\\W]*\\W)", " ");

These dishes are then saved together with every second price (the one for students, the other one’s for employees) in a HashMap, which is then added to a list. For recognizing later, the dishes get the key „dish“ and the prices the key „price“.

Once the data extraction is done, onPostExecute is automatically called. The date is set to a TextView above the ListView and a SimpleAdapter is populating the list of HashMaps into the layout

dateTextView.setText(date);
simpleAdapter = new SimpleAdapter(MainActivity.this,
dishList, R.layout.list, new String[] { "dish",
"price" }, new int[] { R.id.text1, R.id.text2 });
setListAdapter(simpleAdapter);

Since i’s called Minimal Mensa Plan, no other features (such as caching or selecting a cantine) are available. The app in the Play Store is used for scraping the cantine at the campus Berliner Tor but it might as well be used for others, just by changing the URL. It’s released under the MIT License and available at GitHub.

WordPress cache mit redis und uberspace

Posted on 24. Oktober 2014 Comments

Redis ist eine In-Memory Key-Value Datenbank. Das heißt, dass die Daten im Arbeitsspeicher gehalten werden und deswegen besonders schnell verfügbar sind. Dafür sind sie aber auch nicht persistent gespeichert, d.h. bei einem Neustart sind die Daten weg. Das ist aber im Grunde perfekt für einen Cache Speicher.

WordPress ist ja nicht gerade für seine Schnelligkeit bekannt und so gibt es diverse Plugins zum besseren Cachen. Seine eigene Seite kann man z.B. bei Google Page Speed testen. Jedes Mal wenn eine Seite abgerufen wird, muss PHP Daten aus der Datenbank holen und eine Seite generieren. Je nach Hoster, Anbindung etc kann das ganz schön lange dauern.

Die Idee für diesen Post ist Daten, die sich ohnehin nicht so häufig ändern, in einer Redis Datenbank vorzuhalten und statt jedes Mal eine Seite generieren zu lassen einfach diese Daten aus dem Redis Cache abzufragen. Dazu braucht man aber eine Verbindung zwischen PHP und Redis und überhaupt die Möglickeit Redis auf demselben Webserver zu installieren auf dem auch WordPress liegt. Leider bieten das nicht allzu viele Anbieter, mein Anbieter Uberspace jedoch schon. PHP kann über die Bibliotheken Predis oder  PHPRedis auf Redis Server zugreifen.

Das Plugin wp-redis-cache übernimmt jegliche Arbeit. Zum Installieren muss man einfach nur der Anleitung folgen. Die IP seines eigenen Webservers bekommt man z.B. über Ping. Bei Uberspace läuft Redis nicht über TCP sondern über einen Socket. Dieser befindet sich im Home-Verzeichnis ~/.redis/sock.

Als Erstes muss man auf seinem Uberspace Redis installieren:

$ test -d ~/service || uberspace-setup-svscan
$ uberspace-setup-redis

Wenn man statt dem mitgelieferten Predis 5.2 lieber PHPRedis benutzen möchte kann man dies folgendermaßen installieren:

$ uberspace-install-pecl redis

Mit meinem Patch für wp-redis-cache kann man nun auch Sockets mit Predis nutzen.
Wichtig ist nun noch die Variable $redis_server auf folgenden Wert zu setzen.

/home/username/.redis/sock

Die Abkürzung ~/.redis funktioniert hier nicht.

Solange die Variable $debug noch auf true steht, kann man beim Aufruf der Website im Quelltext die gemessene Zeit bis zur Ausgabe sehen. Diese sollte deutlich unter vorherigen Werten liegen.

Im WordPress Backend kann man unter Einstellungen/Wp Redis Cache noch eine maximale Zeit (in Sekunden) angeben, in denen der Cache geleert und neu befüllt wird, um ggf. Änderungen anzuzeigen. Zum Testen kann man aber auch einen $secret_key in der index-wp-redis.php festlegen und seinen Blog so aufrufen: http://blog-url.tld/?refresh=refreshpasswort
Oder man startet einfach Redis neu:

$ svc -du ~/service/redis

Anleitung: Produkte bei eBay über API mit PHP SDK listen – Teil 4: Aufträge löschen, Artikel aktualisieren

Posted on 13. Oktober 2014 Comments

Dieser Blog Post ist Teil der Reihe Produkte bei eBay listen.


4.1. Jobs abbrechen

Es kann immer mal vorkommen, dass es bei der Feed-Erstellung Fehler gibt. Dann wird ggf. von large-merchant-services/02-add-fixed-price-item.php ein Job erstellt. Diesen muss man erst abbrechen um einen neuen Job derselben Art zu erstellen. Dazu braucht man die jobID. Normalerweise wird diese beim Erstellen des Jobs mit angezeigt. Sollte man sie aus irgendeinem Grund nicht zur Verfügung haben, kann man sie aber auch mit large-merchant-services/01-get-jobs.php erfragen. Da hier aber alle Jobs, die jemals ausgeführt wurden, angezeigt werden, muss man noch ein paar kleine Änderungen vornehmen. Als erstes sollte man aber wie auch in Teil 3 sicherstellen, dass man auf der richtigen Plattform arbeitet (hier: Sandbox):

'authToken' => $config['sandbox']['userToken'],
'sandbox' => true

Weiter unten, in der letzten if-Abfrage, sollte man die folgende Line ändern:

$upTo = min(count($response->jobProfile), 500);

Das sollte erstmal reichen. Außerdem kann folgende Zeile hinzufügen

$job = $response->jobProfile[$x]; // alte Zeile (Z. 89 bei mir)
if ($job->jobStatus != "Completed") { // das hier einfügen
...
} // if-Abfrage weiter unten schließen

Jetzt bekommt man nur die Jobs, die noch nicht fertig gestellt sind. Allerdings sind hier auch die Abgebrochenen dabei, also könnte man auch  ein != „Aborted“ hinzufügen.

Hat man nun die jobID, kann man die Vorlage large-merchant-services/01-get-jobs.php ein bisschen abändern. Ich habe die Datei large-merchant-services/04-abort-job.php genannt. Dafür muss in Zeile 58 den richtigen Request und die jobID eingetragen.

$request = new Types\AbortJobRequest(array('jobId' => '4711'));

Eine Zeile darunter:

$response = $service->abortJob($request);

Sollte man das öfter brauchen, sollte es auch nicht allzu schwer sein, die jobID aus den Parametern zu ziehen.

4.2. Preise und Verfügbarkeit aktualisieren

Wie viele andere Marktplätze, gibt eBay auch die Möglichkeit mit einem reduzierten Feed für zuvor gelistete Produkte die Verfügbarkeit und Preise zu updaten. Eine genaue Erklärung der einzelnen Tags findet sich im ersten Teil. Der reduzierte Feed folgendermaßen aus:

<?xml version="1.0" encoding="UTF-8"?>
<BulkDataExchangeRequests>
<Header>
<Version>669</Version>
<SiteID>77</SiteID>
</Header>
<ReviseFixedPriceItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<ErrorLanguage>de_DE</ErrorLanguage>
<WarningLevel>Low</WarningLevel>
<Version>619</Version>

Jetzt kommen die Informationen das Item, also der Eltern-/Parent-Artikel

<Item>
<SKU>26754</SKU>
<InventoryTrackingMethod>SKU</InventoryTrackingMethod>

Mehr ist nicht von nöten, da die Verfügbarkeiten und Preise ja auf Variationsebene festgelegt sind.

<Variations>
<Variation>
<SKU>4711</SKU>
<Quantity>23</Quantity>
<StartPrice>12.34</StartPrice>
</Variation>
<Variation>
<SKU>4712</SKU>
<Quantity>42</Quantity>
<StartPrice>56.78</StartPrice>
</Variation>
... <!-- mehr Variationen hier -->
</Variations>
</Item>
</ReviseFixedPriceItemRequest>
</BulkDataExchangeRequests>

Das war es auch schon. Für das Updaten der Angebote (revise) gibt es noch keine Vorlage von dts. Allerdings kann man das Beispiel für AddFixedPriceItem mit wenigen Änderungen auch dafür benutzen. In Zeile 75 wird statt AddFixedPriceItem einfach ReviseFixedPriceItem eingesetzt:

$createUploadJobRequest->uploadJobType = 'ReviseFixedPriceItem';

Außerdem ändern sich natürlich noch die Dateinamen der hochzuladenen Datei und der Antwort von eBay:

$uploadFileRequest->attachment(file_get_contents(__DIR__.'/ReviseFixedPriceItem.xml.gz'));
...
$tempFilename = tempnam(sys_get_temp_dir(), 'revise-fixed-price-item-responses-').'.zip';

Natürlich kann man auch jede beliebige Änderung über ReviseFixedPriceItem vornehmen. Allerdings funktioniert alles außer Verfügbarkeit und Preis nur, solange noch keine Variation des Artikels gekauft wurde. Manchmal kann es auch gut sein, den Artikel einfach ganz zu löschen und noch einmal neu hochzuladen (Achtung: in Production kann das Geld kosten!). Dafür braucht man den folgenden API Call.

4.3. EndFixedPriceItem

Möchte man ein Angebot komplett löschen, kann man einen einfachen Feed für diese Items schreiben (für Varianten reicht es die Quantity auf 0 zu setzen). Ist bei allen Varianten die Quantity auf 0, wird der Artikel automatisch entfernt (und muss ggf. mit kostenpflichtig wieder erstellt werden), außer man hat beim Einstellen, wie in Teil 1 erwähnt, die Option OutOfStockControl gesetzt. In diesem Fall wird der Artikel bloß ausgeblendet. Die SKU ist hier dementsprechend die Eltern-/Parent-SKU. Der Feed sieht wie folgt aus:

<?xml version="1.0" encoding="utf-8"?>
<BulkDataExchangeRequests xmlns="urn:ebay:apis:eBLBaseComponents">
<Header>
<Version>659</Version>
<SiteID>77</SiteID>
</Header>
<EndFixedPriceItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<EndingReason>NotAvailable</EndingReason>
<SKU>4711</SKU>
<ErrorLanguage>de_DE</ErrorLanguage>
<WarningLevel>High</WarningLevel>
<Version>859</Version>
</EndFixedPriceItemRequest>
...
<!-- Mehr EndFixedPriceItemRequests -->
</BulkDataExchangeRequests >

Das Prinzip ist dasselbe wie bei AddFixedPriceItem und ReviseFixedPriceItem: einfach die Strings im Quellcode ändern und man kann die Datei auch für EndFixedPriceItem benutzen.

Anleitung: Produkte bei eBay über API mit PHP SDK listen – Teil 3: Keys, Sandbox und AddFixedPriceItem

Posted on 13. Oktober 2014 Comments

Dieser Blog Post ist Teil der Reihe Produkte bei eBay listen.


3.1/3.2. Keys und Sandbox

Um die Beispiele von dts ausführen zu können muss man sich am eBay Developer Program anmelden. Man kann hier nicht seinen normalen eBay Account benutzen. Nach der Anmeldung kann man sich jeweils eine DEVID, AppID und CertID für Sandbox und Production erstellen. Erstmal ist es sicher sinnvoll Sandbox Keys zu erstellen. Anschließend musst man sich noch einen User Token generieren lassen. Die mit den Sandbox Keys eingestellten Angebote wird man später unter sandbox.ebay.com bzw. in der deutschen Version finden.

3.3. Vorbereitung des SDK

Abhängigkeiten installieren:

git clone https://github.com/davidtsadler/ebay-sdk-examples.git
curl -sS https://getcomposer.org/installer | php
php composer.phar install

Jetzt müssen nur noch die eben erstellen Keys und der Usertoken in die configuration.php eingetragen werden. Das eigentliche SDK befindet sich in vendor/dts, man muss also nichts anderes von GitHub herunterladen.

3.4. AddFixedPriceItem

Die benötigten Dateien befindet sich im Ordner large-merchant-services, für das Hinzufügen von Artikel wird die 02-add-fixed-price-item.php gebraucht. Sie ist relativ gut verständlich geschrieben. Man sollte darauf achten, dass in den Variablen $exchangeService und $transferService die Sandbox Keys benutzt werden:

'authToken' => $config['sandbox']['userToken'],
'sandbox' => true

Man kann diese Datei mehr oder weniger so benutzen. Sie wird eine .gz-gezippte Datei namens AddFixedPriceItem.xml.gz aus demselben Ordner hochladen. Wenn die .xml-Datei bzw. .gz-Datei wie in Teil 1 und 2 beschrieben erstellt ist und im selben Ordner liegt, kann diese .php-Datei einfach ausgeführt werden. Das Hochladen bzw. Verarbeiten seitens eBay kann eine ganze Weile dauern. Es wird auf jeden Fall eine .xml-Datei gezippt zurückkommen. In ihr findet man ggf. Fehlermeldungen, Warnings und was das Erstellen gekostet hat. In der Sandbox ist dies natürlich kostenlos, es werden 0.0 € berechnet. Es ist deshalb, vor allem bei größeren Tests, wichtig noch einmal die Keys zu überprüfen, so dass nicht ungewollt Kosten entstehen. Möchte man irgendwann umstellen, sehen die beiden Variablen oben folgendermaßen aus:

'authToken' => $config['production']['userToken'],
'sandbox' => false

Außerdem müssen dann in der configuration.php die entsprechenden keys bzw. ein neuer Usertoken für die Production Keys eingetragen sein.

Wird die Datei ausgeführt, die AddFixedPriceItem.xml.gz liegt aber nicht im gleichen Verzeichnis oder wird das Hochladen abgebrochen ist trotzdem ein Job erstellt. Ein weiterer Job des Typs AddFixedPriceItem ist nicht möglich und so muss erst der Job abgebrochen werden. Zu diesen und weiteren nützlichen API, siehe Teil 4.