I need help with my PHP code.
We have an e-commerce site made with wordpress and woocommerce. The site needs to import products from another site's CSV-file through a datafeed.
The datafeed setup requires a PHP-file in the website's file system and a Cron Job that executes the PHP-file.
The PHP-file takes the data from 4dkmiglm.csv (cdn.sandberg.world) and appends it to pricelist_206876.csv (in our system)
These two CSV files differ in number of columns.
The PHP-code looks like this:
`<?php
$current = file_get_contents("https://cdn.sandberg.world/feeds/5dkmiglm.csv");
$pricefile = "/pricelist_206876.csv";
file_put_contents($pricefile,$current);
?>`
I placed the PHP-file in the same folder as the pricelist that needs to be updated but nothing happens. pricelist_206876 is not updated as it should be.
The Cron Job is not in the wp-admin panel, but in the control panel (DirectAdmin) of our web host Inleed. The Cron Job command looks like this: /usr/bin/php -q /dev/null "/home/goffero/domains/goffero.com/datafeedcode.php"
I tried modifying the PHP code in various ways with no result. realpath() didn't work.
I changed the write/read/execute system permissions for the files but that didn't solve the problem.
CodePudding user response:
I have a project in magento and the import of the products is done the same way as you want. We take a csv with 10 or more columns but we export only qty and sku in this case.
Here what we have done
<?php
// downloaded file path
$filesRoot = BP.'/pub/path/to/your/folder/';
//If folder doesn't exist, create it
if (!file_exists($filesRoot)) {
mkdir($filesRoot, 0777, true);
}
//their file
$fileIn = $filesRoot.'5dkmiglm.csv';
//your file
$fileOut = $filesRoot.'pricelist_206876.csv';
$url = 'https://cdn.sandberg.world/feeds/5dkmiglm.csv';
//Download their csv in your environment
if(!file_put_contents($fileIn, file_get_contents($url))) {
die('errore download file');
}
$countLine = 0;
$fileInOpn = fopen($fileIn, 'r');
$fileOutOpn = fopen($fileOut, 'w');
while (($line = fgetcsv($fileInOpn)) !== FALSE) {
//position of each column
if($countLine==0){
$keyQty = array_search('qty', $line);
$keySku = array_search('sku', $line);
}
$newLineRev = [];
foreach ($line as $key => $el) {
if ($key == $keyQty || $key == $keySku) {
$newLineRev[$keyQty] = $line[$keyQty];
$newLineRev[$keySku] = $line[$keySku];
continue;
}
}
// Write in your file
fputcsv($fileOutOpn, $newLineRev);
$countLine ;
}
fclose($fileInOpn);
fclose($fileOutOpn);