简体   繁体   English

如何使用 PHP 使用来自外部 CSV 文件链接的数据更新 CSV 文件?

[英]How do i update a CSV file with data from external CSV file link using PHP?

I need help with my PHP code.我的 PHP 代码需要帮助。

We have an e-commerce site made with wordpress and woocommerce.我们有一个使用 wordpress 和 woocommerce 制作的电子商务网站。 The site needs to import products from another site's CSV-file through a datafeed.该站点需要通过数据馈送从另一个站点的 CSV 文件导入产品。

The datafeed setup requires a PHP-file in the website's file system and a Cron Job that executes the PHP-file.数据馈送设置需要网站文件系统中的 PHP 文件和执行 PHP 文件的 Cron 作业。

The PHP-file takes the data from 4dkmiglm.csv (cdn.sandberg.world) and appends it to pricelist_206876.csv (in our system) PHP 文件从4dkmiglm.csv (cdn.sandberg.world) 获取数据并将其附加到pricelist_206876.csv (在我们的系统中)

These two CSV files differ in number of columns.这两个 CSV 文件的列数不同。

The PHP-code looks like this: PHP 代码如下所示:

`<?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.我将 PHP 文件放在与需要更新的价目表相同的文件夹中,但没有任何反应。 pricelist_206876 is not updated as it should be. pricelist_206876 未按应有的方式更新。

The Cron Job is not in the wp-admin panel, but in the control panel (DirectAdmin) of our web host Inleed. Cron Job 不在 wp-admin 面板中,而是在我们的虚拟主机 Inleed 的控制面板 (DirectAdmin) 中。 The Cron Job command looks like this: /usr/bin/php -q /dev/null "/home/goffero/domains/goffero.com/datafeedcode.php " Cron Job 命令如下所示: /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.我尝试以各种方式修改 PHP 代码,但没有结果。 realpath() didn't work. realpath()没有工作。

I changed the write/read/execute system permissions for the files but that didn't solve the problem.我更改了文件的写/读/执行系统权限,但这并没有解决问题。

I have a project in magento and the import of the products is done the same way as you want.我在 magento 中有一个项目,产品的导入按照您想要的方式完成。 We take a csv with 10 or more columns but we export only qty and sku in this case.我们采用包含 10 列或更多列的 csv,但在这种情况下我们仅导出 qty 和 sku。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM