Frage

I'm creating a CMS currently and one of the major features of the CMS is a datafeed system. The website is to transfer the contents of one of its databases to a large number of listing sites. Each site has its own specifications for formatting of this information, and I've been tasked with creating a backend which can be used to easily modify and add datafeeds for non-programmers.

So far, out of the formats I've recieved there three file types, XML, CSV, and TXT. There are different standards for formatting even inside these file types, different ordering of fields, some have quotations, some don't, etc. I've been puzzling over this for a while and here is my solution:

  • Each feed will have a template stored in a separate database table. The templates will consist of whatever structure the feed requires (XML, CSV, TXT) with placeholder value (ex. {{NAME}}). A script will then loop through each database entry, replace the placeholders with the variable values, and save the completed document with the correct file extension.

My issue is figuring out how to save multiple files using one PHP file (perhaps calling the same file multiple times from another PHP file?) and moreover, how to save different file types like this. Basically, how do I set the extension and save the file?

War es hilfreich?

Lösung

You save multiple files like you save the first one.

If you have the files contents in $filecontent, and the filename to be used (with appropriate extension) in $filepath, you can use

file_put_contents($filename, $filecontent)

Do that in your loop and you’re done.

For details on file_put_contents, see its php manual page.

Andere Tipps

I'd recommend an object oriented approach to all of this:

1) Create interfaces for each datatype to which you need to convert each object

interface xmlSerializable {
    public function toXML();
}

interface csvSerializable {
    public function toCSV();
}

interface txtSeriablizable() {
    public function toTXT();
}

2) Create a class to represent the type of data that you need to serialize into the different formats for your clients and implement each of the interfaces

class Data implements xmlSerializeable { // I only implemented one for brevity
    private $id         = null;
    private $stuff      = null;
    private $otherStuff = null;
    private $stuffArray = array();

    public __construct($id, $stuff, $otherStuff, $stuffArray) {
        $this->id         = $id;
        $this->stuff      = $stuff;
        $this->otherStuff = $otherStuff;
        $this->stuffArray = $stuffArray;
    }

    public function getId() { return $this->id; }

    public function toXML() {
        $output = '<?xml version="1.0" encoding="UTF-8"?>'."\n".
                  '<data>'."\n\t".
                  '<id>'.$this->id.'</id>'."\n\t".
                  '<stuff>'.$this->stuff.'</stuff>'."\n\t".
                  '<otherStuff>'.$this->otherStuff.'</otherStuff>'."\n\t".
                  '<stuffArray>'."\n\t\t";

        foreach($this->stuffArray as $stuff) {
            $output .= '<stuff>'.$stuff.'</stuff>'."\n\t\t";
        }

        $output .= '</stuffArray>'."\n".
                   '</data>';

        return $output;
    }
}

Now you can create Data objects from your database by creating a DataFactory that accepts a SQL query and returns an array of Data objects. To serialize them, just call the method you implemented for each format:

$df    = new DataFactory($pdo);
$datas = $df->query('SELECT * FROM Data');

foreach($datas as $data) {
    file_put_contents('/data/xml/'.$data->getId().'.xml', $data->toXML());
    // You can add other formats here in the above fashion
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top