POS Integrations
FTP gateway
Code examples

Code examples

PHP (ext-ftp)

This example needs the PHP FTP extension (opens in a new tab) to be installed. It creates a CSV file in memory based on an array of stock data.

<?php
 
// Define shop upload key and inventory
$upload_key = '<<your upload key>>';
$inventory = [
	[
		'barcode' => '2001234567',
		'price' => '1.99',
		'quantity' => 24,
		'currency' => 'USD'
	],
	// ...
];
 
// Connect to FTP server
$connection = ftp_connect('ftp.near.live');
ftp_login($connection, 'apikey', $upload_key);
ftp_pasv($connection, true);
 
// Turn array into CSV
$handle = fopen('php://temp', 'r+');
ftruncate($handle, 0);
fputcsv($handle, array_keys($inventory[0]));
foreach ($inventory as $item) {
	fputcsv($handle, array_values($item));
}
rewind($handle);
 
// Upload via FTP as stock.csv
ftp_fput($connection, 'stock.csv', $handle, FTP_BINARY, 0);
 
// Close file handle and connection
fclose($handle);
ftp_close($connection);

Javascript (Node.js)

This example uses the ftp-deploy (opens in a new tab) package to upload the stock file to the FTP server. It expects a file named stock.csv to exist in the script's directory.

const FtpDeploy = require('ftp-deploy');
const ftpDeploy = new FtpDeploy();
 
const uploadKey = '<<your upload key>>';
const stockFileName = 'stock.csv';
 
const config = {
  user: 'apikey',
  password: uploadKey,
  host: 'ftp.near.live',
  port: 21,
  localRoot: __dirname,
  remoteRoot: '/',
  include: [stockFileName],
  deleteRemote: false,
  forcePasv: true
};
 
ftpDeploy.deploy(config, function (err, files) {
  if (err) {
    console.log(err);
    process.exit(1);
    return;
  }
 
  console.log('Uploaded files', files);
});

PowerShell

A great low-tech way to get file uploads working in a PowerShell script that you execute on a timer (opens in a new tab).

# Update these variables
$csvPath = "C:\local\path\stock.csv" # Point this to your CSV file
$uploadKey = "xxxxxxxxx" # Set your Upload key / FTP password here
 
# Everything below here can stay the same
$request = [Net.WebRequest]::Create("ftp://ftp.near.live/stock.csv")
$request.Credentials = New-Object System.Net.NetworkCredential("apikey", $uploadKey)
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile 
 
$fileStream = [System.IO.File]::OpenRead($csvPath)
$ftpStream = $request.GetRequestStream()
 
$fileStream.CopyTo($ftpStream)
 
$ftpStream.Dispose()
$fileStream.Dispose()