Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active October 13, 2015 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjn/4136595 to your computer and use it in GitHub Desktop.
Save tomjn/4136595 to your computer and use it in GitHub Desktop.
Grab product orders from Sellwire in PHP
require 'PHP-Browser/browser.class.php';
function get_sellwire_csv( $username, $password, $project_id ) {
$browser = new Browser( 'SellWire Browser' );
$browser->navigate( 'http://app.sellwire.net/logout' );
$browser->navigate( 'http://app.sellwire.net/login' );
$form = $browser->getForm( '//form[@method]' );
$form->setAction( 'http://app.sellwire.net/login' );
$form->setAttributeByName( 'username', $username )->setAttributeByName( 'password', $password );
$browser->submitForm( $form, 'login' );
$browser->click( "//a[@href='http://app.sellwire.net/orders']" );
$form = $browser->getForm( "//form[@action='http://app.sellwire.net/orders/export']" );
$form->setAttributeByName( 'export_file', $project_id );
$browser->submitForm( $form, 'Export' );
$csv = $browser->getRawResponse();
return $csv;
}
class SellWire_CSV_Sale {
private $values = array();
public function get( $key ){
return $this->values[$key];
}
public function set( $key, $value ){
$this->values[$key] = $value;
}
public function parse_raw_line( $line ) {
$columns = array(
'file_name',
'order_id',
'trans_id',
'customer_name',
'customer_email',
'amount',
'tax',
'total',
'currency',
'in_eu',
'tax_country',
'tax_number',
'ordered_on',
'downloads',
'views',
'notes',
'status'
);
$lines = explode( ',', $line );
$iteration = 0;
foreach ( $columns as $column ) {
$this->set( $column, $lines[$iteration] );
$iteration++;
}
}
}
function get_sellwire_sales( $username, $password, $project_id ){
$csv = get_sellwire_csv( $username, $password, $project_id );
$lines = explode( "\n", $csv );
$sales = array();
foreach ( $lines as $line ) {
if ( empty( $line ) ) {
continue;
}
$sale = new SellWire_CSV_Sale();
$sale->parse_raw_line( $line );
if ( $sale->get( 'file_name' ) == '"file_name"' ) {
continue;
}
$sales[] = $sale;
}
return $sales;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment