Friday, May 13, 2016

Sears API

Sears New API:
About time they begin to work on this.


 class SearsComm  
 {  
      private $email = '';  
      private $timestamp = null;  
      private $seller = '';  
      private $key = '';  
      function __construct()  
      {  
           date_default_timezone_set('Etc/GMT');  
           $this->timestamp = date('Y-m-d\TH:i:s\Z', time());  
           sleep(5);//needed cause Sears appears to be a little slow  
      }  
      function getSears($url)  
      {  
           $curl = curl_init($url);  
           curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");  
           curl_setopt($curl, CURLOPT_HEADER, false);  
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
           curl_setopt($curl, CURLOPT_HTTPHEADER, array($this->createSig()));  
           curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , false);//should really use a perm file instead  
           // Make the REST call, returning the result  
           $response = curl_exec($curl);  
           curl_close($curl);  
           if (!$response) {  
                die("Connection Failure.");  
           }  
           else  
                return $response;  
      }  
      function putSears($url, $data)  
      {  
           $curl = curl_init($url);  
           curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");  
           curl_setopt($curl, CURLOPT_HEADER, false);  
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
           curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $this->createSig()));  
           curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  
           curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , false);//should really use a perm file instead  
           // Make the REST call, returning the result  
           $response = curl_exec($curl);  
           curl_close($curl);  
           if (!$response) {  
                die("Connection Failure.");  
           }  
           else  
                echo $response;  
      }  
      function createSig()  
      {  
           return "authorization:HMAC-SHA256 emailaddress=".$this->email.",timestamp=".$this->timestamp.",signature=" . hash_hmac ( 'sha256' , $this->seller.":".$this->email.":".$this->timestamp, $this->key);  
      }  
 }  

Friday, April 22, 2016

Dump an SQL query

This is specifically mysqli php dump.  Saved my some time in formatting outputs of internal scripts.  Not really suitable for front end dev.


 function printTable(&$result)  
 {  
      $finfo = $result->fetch_fields();  
      echo '<table class="striped">' . "\n";  
      echo '<tr class="header">' . "\n";  
      foreach ($finfo as $val) {  
           echo "<td>".$val->name."</td>\n";  
      }  
      echo "</tr>\n";  
      while($row = $result->fetch_assoc()) {  
           echo "<tr>";  
           foreach ($row as $col) {  
                echo "<td>".$col."</td>";  
           }  
           echo "</tr>\n";  
      }  
      echo "</table>\n";  
      $result->free();  
 }  

Wednesday, February 17, 2016

Magento2 adminhtml wysiwyg in Custom Module

Just made my first magento2 module to store a bunch of site specific data.  Was having a bear of a time figuring out how to enable the wysiwyg editor.  A lot of google'd results were completely irrelevant or wrong.  Some were even extremely complicated, but alas, its super super super easy.

In your /edit

->addField(
            'item_name',
            'text', array(

in your little array of data variables, just add 'wysiwyg' => true
Thats it! no adding calls to the framework, no constructors needed or other extended classes, just 1 variable.

to be clear, here is a fully flushed out field.

     $fieldset->addField(  
       'menu',  
       'editor',  
       ['name' => 'menu',   
       'label' => __('Menu HTML'),   
       'title' => __('Menu HTML'),   
       'required' => true,  
       'wysiwyg' => true]  
     );  

So much simpler then people lead you on to believe.  Figured it out by reading the source code instead of all the bloggers and tutorials.  Go figure -.-

Tuesday, February 16, 2016

Parsing the Web

We are doing business with a rather large company involving over 1300 websites/eCommerce stores.  Unfortunately, its only a large company due to overwhelming large bureaucracy, and not actual success, so, we can't actually find someone to get us data THEY need for their own sites. So, I stepped in and decided to decrypt their own architecture for them.

Where to start?  Same place you start for everything.  Wiki


 function parsewikipage(&$file)  
 {  
      $wiki = new DOMDocument();  
      $wiki->loadHTMLFile('https://en.wikipedia.org/wiki/hidden');  
      $content = $wiki->getElementByID('mw-content-text');  
      $elements = $content->getElementsByTagName('li');  
      foreach($elements as $element)  
      {  
           if($element->getAttribute('id')=='cite_note-1')  
                break;  
           if($element->getAttribute('class')=='' && $element->getAttribute('id')=='')  
                if (!is_null($element)) {  
                 $tags = $element->getElementsByTagName('a');  
                 if (!is_null($tags[0])) {  
                      getsiteurl('https://en.wikipedia.org'.$tags[0]->getAttribute('href'), $file);  
                 }  
                }  
      }  
 }  
 function getsiteurl($url, &$file)  
 {  
      //echo $url."\n";  
      $dom = new DOMDocument();  
      $dom->loadHTMLFile($url);  
      if(!is_null($dom))  
      {  
           $content = $dom->getElementByID('mw-content-text');  
           if(!is_null($content))  
           {  
                $elements = $content->getElementsByTagName('table');  
                $details = $elements[0]->getElementsByTagName('tr');  
                foreach($details as $detail)  
                     if($detail->childNodes[0]->nodeValue=='Website')  
                     {  
                          getsiteid($detail->getElementsByTagName('a')[0]->getAttribute('href'), $file);  
                          break 1;  
                     }  
           }  
      }  
 }  
A snippet of the tree look-ups and beyond that there is even parsing of some text string in some web pages further down to compile all the content we needed.

Realizing the people your talking to knowing absolutely nothing about what they need can be horrifying, but, ignoring them and acquiring all the data "magically" has substantial time downsides, but does increase the way people view you (assuming your not dealing with lawyers or trolls).


Friday, February 5, 2016

Start of a Dev Diary

Starting a development Diary for work.  Partly to track my own time and partly to remind my self what the hell i did last week.

Today started off working on the dev machine with the Magento2 install to push the testing theme forward to the default theme.  Apache changes were easy, Magento has weird caching/reindexing things that make it seem like you failed, but leaving it alone for a few moments fixes everything (assuming you have your crons setup).  A few touches to pathings that went astray and that was taken care of.

Latter half of the day was spent building up a php interface to the Core Commerce ERP api.  Only have 1 client on-boarding using this system and it seems its not super popular, so not spending too much time on it, but I am making everything generic; better safe than sorry.

Below is a basic communication class, mostly for testing purposes at this point.  These a logic script above this that handles data parsing/input/output etc etc

 <?php  
 class CoreCommerceCom  
 {  
      private $url = null;  
      private $proxytunnel = false;  
      private $curlProxy = 0;   
      private $username = null;  
      private $password = null;  
      private $storeName = null;  
      private $key = null;  
        
      function __construct($config)  
      {  
           $this->url                =     $config->url;  
           $this->proxytunnel     =     $config->proxytunnel;  
           $this->curlProzy     =     $config->curlProxy;  
           $this->username          =     $config->username;  
           $this->password          =     $config->password;  
           $this->storeName     =     $config->storeName;  
           $this->key               =     $config->key;            
      }  
        
      private function sendRequest($request)  
      {  
           $request=createCall($request);  
             
           $curl = curl_init();  
           curl_setopt($curl, CURLOPT_URL, $this->url);  
              
           curl_setopt($curl, CURLOPT_HEADER, false);  
           curl_setopt($curl, CURLOPT_PORT, '443');  
           curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  
           curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
           curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );  
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
           curl_setopt($curl, CURLOPT_TIMEOUT, 30);  
           curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);  
              
           if($this->proxytunnel === true){  
                curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, TRUE);  
                curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);  
                if($curlProxy) {  
                     curl_setopt($curl, CURLOPT_PROXY, $curlProxy);  
                }  
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  
           }  
           curl_setopt($curl, CURLOPT_POSTFIELDS, $request);  
    
           $response = curl_exec($curl);  
           if(false === $response){  
                $err = curl_error($curl);  
                $errno = curl_errno($curl);  
                print 'cURL error ' . $errno . ': ' . $err;  
           }   
           curl_close($curl);  
           return $response;  
      }  
        
      private function createCall($action)  
      {  
           return '<?xml version="1.0" encoding="utf-8"?>  
                <Request version="1.0">  
                     <Authentication>  
                          <Username>' . $this->username . '</Username>  
                          <Password>' . $this->password . '</Password>  
                          <StoreName>' . $this->storeName . '</StoreName>  
                          <XmlKey>'. $this->key . '</XmlKey>  
                     </Authentication>  
                     '.$action.'  
                </Request>';  
      }  
        
      function getOrdersByDate($from, $to)  
      {  
           $xml='<SearchCriteria type="Like">  
           <DateRanges>  
                <DateRange>  
                     <From>'.$from.'</From>  
                     <To>'.$to.'</To>  
                </DateRange>  
           </DateRanges>  
           </SearchCriteria>';  
           return sendRequest($xml);  
      }  
        
      function setTracking($externalid, $tracking, $carrier, $method)  
      {  
           $xml='<Action>ACTION_TYPE_ORDER_EDIT</Action>  
                <List length="1">  
                     <Order id="'.$externalid.'">  
                     <Status>ORDER_STATUS_SHIPPED</Status>  
                          <ShippingDate>  
                               <UnixTimeStamp>1235148507</UnixTimeStamp>  
                          </ShippingDate>  
                          <TrackingElement>  
                               <Number><![CDATA['.$tracking.']]></Number>  
                               <Service><![CDATA['.$carrier.']]></Service>  
                               <Method><![CDATA['.$method.']]></Method>  
                          </TrackingElement>  
                     </Order>  
                </List>';  
           return sendRequest($xml);  
      }  
        
      function setInventory($externalprodid, $inv)  
      {  
           $xml='<Action>ACTION_TYPE_PRODUCT_EDIT</Action>  
                <List length="1">  
                     <Product id="'.$externalprodid.'">  
                          <InventoryLevel><![CDATA['.$inv.']]></InventoryLevel>  
                     </Product>  
                </List>';  
           return sendRequest($xml);  
      }  
 }