• Stop being a LURKER - join our dealer community and get involved. Sign up and start a conversation.

Reply to thread

Some of you guys will get this. Some of you won't. Some of you will see it and keep scrolling. For those of you who do get it, It's pretty cool with the right set up!!


[PHP]<?php


csvToJson("yourInventoryCSVfile.csv");

// php function to convert csv to json format

function csvToJson($fname) {

    // open csv file

    if (!($fp = fopen($fname, 'r'))) {

        die("Can't open file...");

    }

   

    //read csv headers

    $key = fgetcsv($fp,"1024",",");

   

    // parse csv rows into array

    $json = array();

        while ($row = fgetcsv($fp,"1024",",")) {

        $json[] = array_combine($key, $row);

    }

   

    // release file handle

    fclose($fp);

   

        //write to json file in same dir. Change the filename below to whatever you like!

    $fp2 = fopen('yourJSONfile.json', 'w');

    fwrite($fp2, json_encode($json));

    fclose($fp2);

   

    // encode array to json

    return json_encode($json);

}

?>[/PHP]


Now get with your inventory provider and have them to FTP your inventory file to the directory this PHP file sits in. Go into your server and set up a Cron Job to run on a schedule and now all your inventory will be sitting in a JSON formatted file that you can access via scripts on your website!! I know it's kinda old school but... It works!!  Ol ya, I forgot one thing. You need to set up CORS to access it.


Create a htaccess file and put it in the same directory! This will do the trick:

Header set Access-Control-Allow-Origin "*"


You can make it URL specific for access if you want.