Aqui está um código útil que uso em um de meus projetos com uma configuração de várias lojas PRESTASHOP (v1.5.6) para importar produtos automaticamente para cada loja. Primeiro, eu gerei o arquivo CSV com os dados do produto que desejo inserir ou atualizar, em seguida, o script faz login como você faria na área de administração prestashop e POST os dados no controlador AdminImport.
Aqui está o script de background / cron e abaixo da classe curl wrapper (você pode substituir o curl wrapper php, é claro)
require_once ('class.PSRequest.php'); //see class source code below
$shopID = "myfirstshop"; // ID or unqiue name of the target shop
$products = ...; // your set of product object or array
// CSV first line for products
$csv = "id;Active (0/1);Name*;Categories (x,y,z,...);Price tax excluded;Tax rules id;Wholesale price;On sale (0/1);Discount amount;Discount percent;Discount from (yyy-mm-dd);Discount to (yyy-mm-dd);Reference #;Supplier reference #;Supplier;Manufacturer;EAN13;UPC;Ecotax;Weight;Quantity;Short description;Description;Tags (x,y,z,...);Meta-title;Meta-keywords;Meta-description;URL rewritten;Text when in-stock;Text if back-order allowed;Available for order (0 = No, 1 = Yes);Product creation date;Show price (0 = No, 1 = Yes);Image URLs (x,y,z,...);Delete existing images (0 = No, 1 = Yes);Feature (Name:Value:Position);Available online only (0 = No, 1 = Yes);Condition (new,used,refurbished);ID / Name of shop".PHP_EOL;
foreach ($products as $prod) {
// fill in the csv string with your datas
$csv .= $prod->id.";1;".html_entity_decode($prod->name)......$shopID.PHP_EOL;
}
$now=time();
$csvname = $now."-PRODUCTS-".$shopID.".csv";
$file = YOUR_PATH.$csvname;
file_put_contents($file, $csv);
echo "Write file : ".$file.PHP_EOL;
echo "Login on Prestashop Admin area...".PHP_EOL;
$adminUrl = "http://YOUR_PS_ADMIN/adminXXXX/"; // change that with your admin url of course
$request = new PSRequest();
$request->setCookiFileLocation(__DIR__.'/PScookie.txt');
$request->setPost(array("email" => "YOUR_ADMIN_EMAIL","passwd" => "YOUR_ADMIN_PASS", "submitLogin" => "Connexion")); // you must be a super admin
$request->call($adminUrl."index.php?controller=AdminLogin");
echo "Get token...".PHP_EOL;
$request->call($adminUrl."index.php?controller=AdminImport");
list(,$response) = explode("rnrn", $request->_webpage, 2);
preg_match("/token=([a-z0-9]+)/", $response, $matches);
$token = $matches[1];
echo "Token : ".$token.PHP_EOL;
// Send POST datas just like the admin form would do it, those datas depends on what you want to do : check the import admin page.
$request->setPost(array(
"controller" => "AdminImport",
"token" => $token,
"skip" => 1,
"csv" => $csvname,
"convert" => '',
"regenerate" => '',
"entity" => 1, //1 is for products import
"iso_lang" => "fr",
"truncate" => 0,
"forceIDs" => 1,
"match_ref" => 1,
"separator" => ";",
"multiple_value_separator" => ",",
"import" => 1,
"type_value" => array( 0 => 'id', 1 => 'active', 2 => 'name', 3 => 'category', 4 => 'price_tex', 5 => 'id_tax_rules_group', 6 => 'wholesale_price', 7 => 'on_sale', 8 => 'reduction_price', 9 => 'reduction_percent', 10 => 'reduction_from', 11 => 'reduction_to', 12 => 'reference', 13 => 'supplier_reference', 14 => 'supplier', 15 => 'manufacturer', 16 => 'ean13', 17 => 'upc', 18 => 'ecotax', 19 => 'weight', 20 => 'quantity', 21 => 'description_short', 22 => 'description', 23 => 'tags', 24 => 'meta_title', 25 => 'meta_keywords', 26 => 'meta_description', 27 => 'link_rewrite', 28 => 'available_now', 29 => 'available_later', 30 => 'available_for_order', 31 => 'date_add', 32 => 'show_price', 33 => 'image', 34 => 'delete_existing_images', 35 => 'features', 36 => 'online_only', 37 => 'condition', 38 => 'shop')
)
);
echo "call AdminImport and POST datas...".PHP_EOL;
$request->call($adminUrl."index.php?controller=AdminImport&token=".$token);
echo "-- END --".PHP_EOL;
$request = null;
Aqui está a classe curl do wrapper
class PSRequest {
protected $_useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';
protected $_cookieFileLocation = './cookie.txt';
protected $_referer = "http://www.google.com";
protected $_url;
protected $_followlocation;
protected $_timeout;
protected $_maxRedirects;
protected $_post;
protected $_postFields;
protected $_session;
protected $_includeHeader;
protected $_noBody;
protected $_status;
protected $_binaryTransfer;
public $_webpage;
public $authentication = 0;
public $auth_name = '';
public $auth_pass = '';
protected $ch; // curl handler
public function __construct($url = '', $followlocation = true, $timeOut = 30, $maxRedirecs = 4, $binaryTransfer = false, $includeHeader = true, $noBody = false)
{
$this->_url = $url;
$this->_followlocation = $followlocation;
$this->_timeout = $timeOut;
$this->_maxRedirects = $maxRedirecs;
$this->_noBody = $noBody;
$this->_includeHeader = $includeHeader;
$this->_binaryTransfer = $binaryTransfer;
$this->_cookieFileLocation = dirname(__FILE__).'/cookie.txt';
$this->ch = curl_init();
}
public function __destruct() {
curl_close($this->ch);
}
public function useAuth($use){
$this->authentication = 0;
if($use == true) $this->authentication = 1;
}
public function setName($name){
$this->auth_name = $name;
}
public function setPass($pass){
$this->auth_pass = $pass;
}
public function setReferer($referer){
$this->_referer = $referer;
}
public function setCookiFileLocation($path)
{
$this->_cookieFileLocation = $path;
}
public function setPost($postFields)
{
$this->_post = true;
if (is_array($postFields)) {
$fields_string = http_build_query($postFields);
}
else {
$fields_string = $postFields;
}
$this->_postFields = $fields_string;
}
public function setUserAgent($userAgent)
{
$this->_useragent = $userAgent;
}
public function call($url = null)
{
if ($url) {
$this->_url = $url;
}
if (!$url) {
throw new Exception('You should set an URL to call.');
}
curl_setopt($this->ch,CURLOPT_URL,$this->_url);
curl_setopt($this->ch,CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($this->ch,CURLOPT_TIMEOUT,$this->_timeout);
curl_setopt($this->ch,CURLOPT_MAXREDIRS,$this->_maxRedirects);
curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($this->ch,CURLOPT_FOLLOWLOCATION,$this->_followlocation);
curl_setopt($this->ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->ch,CURLOPT_COOKIESESSION, true );
curl_setopt($this->ch,CURLOPT_COOKIEJAR,$this->_cookieFileLocation);
curl_setopt($this->ch,CURLOPT_COOKIEFILE,$this->_cookieFileLocation);
if ($this->authentication == 1) {
curl_setopt($this->ch, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass);
}
if ($this->_post) {
curl_setopt($this->ch,CURLOPT_POST,true);
curl_setopt($this->ch,CURLOPT_POSTFIELDS,$this->_postFields);
}
if ($this->_includeHeader) {
curl_setopt($this->ch,CURLOPT_HEADER,true);
}
if ($this->_noBody) {
curl_setopt($this->ch,CURLOPT_NOBODY,true);
}
curl_setopt($this->ch,CURLOPT_USERAGENT,$this->_useragent);
curl_setopt($this->ch,CURLOPT_REFERER,$this->_referer);
$this->_webpage = curl_exec($this->ch);
$this->_status = curl_getinfo($this->ch,CURLINFO_HTTP_CODE);
return $this->_webpage;
}
public function getHttpStatus()
{
return $this->_status;
}
public function __tostring(){
return $this->_webpage;
}
}
e é isso. Basta configurar seu cron como este, por exemplo:
@daily /usr/bin/php /path/to/my_script.php > /dev/null 2>&1
Você pode adicionar um log para a lógica do arquivo ao script para verificar se tudo correu bem!