Skip to main content

Search

Search

Json?

Comments

6 comments

  • Avatar
    Ian Cummings
    Moderator

    Nothing that I know of I'm afraid.  I've added it to the official wish list and so has visibility to our developers.

    0
  • Avatar
    Legacy Poster

    fwiw, no warranty expressed or implied

     

    <?php
    
    // xmlpp function snarfed from http://gdatatips.blogspot.com/2008/11/xml-php-pretty-printer.html
    function xmlpp($xml, $html_output=false) {
    //folded
    }
    
    function requestBTXML($request,$host,$port) {
            $fp = fsockopen($host, $port, $errno, $errstr, 3);
            if (!$fp) {
                    throw new Exception($errstr . '(' . $errno . ')');
            } else {
            while (!feof($fp)) {
            $prompt = fgets($fp);
            if (strpos($prompt,'Greetings') !== false) break;
            }
            $wstatus = fwrite($fp, $request);
            while (!feof($fp)) {
            $response = fgets($fp);
                            if (strpos($response,'Thanks') !== false) break;
            }
            fclose($fp);
            }
    
    }
    
    function JSON2btXML($labelJSON) {
    
            //template for the Bartender XMLScript XML file to be created
            $ltpl = <<<EOD
    <XMLScript Version="2.0">
      <Command>
        <Print>
        </Print>
      </Command>
    </XMLScript>
    EOD;
    
            //example of data input format
            //$label1->cmdname = "My Bartender Job";
            //$label1->format = "c:\labels\mylabelformat.btw";
            //$label1->fields = array(
            //              'name' => 'mark',
            //              'addr' => 'ormond');
    
            //note that backslashes must be escaped twice like this
            //$label2j = '{"cmdname":"My Bartender Job","format":"c\\\\:labels\\\\mylabelformat.btw","fields":{"name":"mark","addr":"ormond"}}';
    
            $labeld = json_decode($labelJSON);
            if (json_last_error() > 0) throw new Exception('JSON Decode Error ' .json_last_error());
    
            $sxe = new SimpleXMLElement($ltpl);
    
            $format = $sxe->Command->addAttribute('Name',$labeld->cmdname);
            $format = $sxe->Command->Print->addChild('Format',$labeld->format);
            foreach ($labeld->fields as $key => $value) {
                    $t = $sxe->Command->Print->addChild('NamedSubString');
                    $t->addAttribute('Name', $key);
                    $t->addChild('Value',$value);
            }
    
            return xmlpp($sxe->asXML());  //use this for pretty xml
            //return $sxe->asXML(); //this will work too
    
    }
    
    // Main Start here
    
    $DEB = True;
    
    $DEB && error_log('REQ MTD is ' . $_SERVER['REQUEST_METHOD'] . ' in ' . __FILE__);
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        global $DEB;
            $labelJSON = $_POST['restData'];
            $BTHOST = $_POST['btHost'];
            $BTPORT = $_POST['btPort'];
            $DEB && error_log('Host ' . $BTHOST . ' Port ' . $BTPORT . ' in ' . __FILE__);
            $DEB && error_log('JSON data is ' . $labelJSON . ' in ' . __FILE__);
            $BTXML = JSON2btXML($labelJSON);
            $DEB && error_log('XML data is ' . $BTXML . ' in ' . __FILE__);
            requestBTXML($BTXML,$BTHOST,$BTPORT);
    }
    
    ?>
    
    

     

     

    0
  • Avatar
    Legacy Poster

    And the setup for the tcpip protocol messages.. well it's at http://i.imgur.com/YnXKuFm.png .  I can't link to it from here.

    0
  • Avatar
    Ian Cummings
    Moderator

    FYI: Images can be inserted into your replies by clicking the "More Reply Options" button.  External websites ware white listed to deter spammers.

    0
  • Avatar
    Legacy Poster

    Here's the setup for the TCIP protocol in the attached image.

    0
  • Avatar
    Ian Cummings
    Moderator

    As an update, BarTender 2016 and the Integrations Platform (that replaces Commander) now allows for web service events, and can consume JSON data content.

     

    http://www.seagullscientific.com/label-software/barcode-label-design-and-printing/

    0

Please sign in to leave a comment.