Skip to content

Instantly share code, notes, and snippets.

@danielkmariam
Created January 4, 2016 16:20
Show Gist options
  • Save danielkmariam/923c3546751a9d2f1589 to your computer and use it in GitHub Desktop.
Save danielkmariam/923c3546751a9d2f1589 to your computer and use it in GitHub Desktop.
<?php
// Initialize
$app = new SimpleHttpClient('http://uk.sodastream.int.sessiondigital.com/xmlconnect/');
$app->addCookie('app_code', 'uk_iph6');
$app->addCookie('screen_size', '600x400');
// Get gas cylinder products
echo "\n[REQUEST] Fetching product catalog...\n";
$response = $app->request('gascylinderlist');
if ((string)$response->status == 'error') {
echo "Error: " . (string)$response->text . "\n";
exit(1);
}
// Select the first product from list
$productId = (int)$response->xpath('/category/products/item[1]/entity_id')[0];
// Add this product to the cart
echo "\n[REQUEST] Adding product $productId to cart...\n";
$response = $app->request('cart/add/product/' . $productId);
if ((string)$response->status != 'success') {
echo "Error while adding product to cart";
exit(1);
}
echo "Message: " . (string)$response->text . "\n";
// get billing address form fields
echo "\n[REQUEST] Getting billing address form fields...\n";
$response = $app->request('checkout/newBillingAddressForm');
foreach ($response->field as $field) {
echo " - {$field['name']}\n";
}
$address = array(
'firstname' => 'John',
'lastname' => 'Doe',
'email' => 'john.doe@example.com',
'street' => array('Street 1'),
'city' => 'London',
'country_id' => 'GB',
'region_id' => 'London',
'postcode' => 'sw82pa',
'telephone' => '4152300694',
'fax' => '',
'save_in_address_book' => ''
);
// billing address
echo "\n[REQUEST] Posting billing address...\n";
$response = $app->request('checkout/saveBillingAddress', 'POST', array('billing' => $address));
if ((string)$response->status != 'success') {
echo "Error while saving billing address";
exit(1);
}
echo "Message: " . (string)$response->text . "\n";
// shipping address
echo "\n[REQUEST] Posting shipping address...\n";
$response = $app->request('checkout/saveShippingAddress', 'POST', array('shipping' => $address));
if ((string)$response->status != 'success') {
echo "Error while saving shipping address";
exit(1);
}
echo "Message: " . (string)$response->text . "\n";
// Delivery type
echo "\n[REQUEST] Posting delivery type...\n";
$response = $app->request('delivery/saveDeliveryType', 'POST');
if ((string)$response->status != 'success') {
echo "Error while saving delivery type";
exit(1);
}
echo "Message: " . (string)$response->text . "\n";
// Get shipping methods
echo "\n[REQUEST] Fetching available shipping methods using shippingMethodsList...\n";
$response = $app->request('checkout/shippingMethodsList');
echo "\n $response\n";
$shippingMethods = array();
foreach ($response->method->rates->rate as $rate) {
$shippingMethods[] = (string)$rate['code'];
echo " - {$rate['code']}\n";
}
echo "Message: " . (string)$response->text . "\n";
if(!$shippingMethods[0]) {
die('Shipping method not found');
}
// Save shipping method
echo "\n[REQUEST] Saving shipping method...\n";
$response = $app->request('checkout/saveShippingMethod', 'POST', array('shipping_method' => $shippingMethods[0]));
if ((string)$response->status != 'success') {
echo "Error while saving shipping method";
exit(1);
}
echo "Message: " . (string)$response->text . "\n";
// Get payment methods
echo "\n[REQUEST] Fetching available payment methods...\n";
$response = $app->request('checkout/paymentMethodList');
$paymentMethods = array();
foreach ($response->method_list->method as $method) {
$paymentMethods[] = (string)$method['code'];
echo " - {$method['code']}\n";
}
if (!in_array('checkmo', $paymentMethods)) {
echo "This demo only supports 'checkmo'. Please enable 'Check / Money Order' in configuration.";
}
// save payment method
echo "\n[REQUEST] Saving payment method...\n";
$response = $app->request('checkout/savePayment', 'POST', array('payment' => array('method' => 'checkmo')));
if ((string)$response->status != 'success') {
echo "Error while saving payment method";
exit(1);
}
echo "Message: " . (string)$response->text . "\n";
// Order review
echo "\n[REQUEST] Fetching order review...\n";
$response = $app->request('checkout/orderReview');
echo "Products:\n";
foreach ($response->products->item as $item) {
echo " - {$item->qty}x {$item->name}\n";
}
echo "Totals:\n";
foreach ($response->totals->children() as $total) {
echo " - {$total->title}: {$total->formated_value}\n";
}
// Place order
echo "\n[REQUEST] Place order...\n";
$response = $app->request('checkout/saveOrder', 'POST', array(
'payment' => array('method' => 'checkmo'),
'agreement' => array('1' => 1)
));
if ((string)$response->status != 'success') {
echo "Error while saving order";
exit(1);
}
echo "Message: " . (string)$response->text . "\n";
echo "Order ID: " . (string)$response->order_id . "\n";
/**
* Simple HTTP client for demonstration purposes
*
* @author Fabrizio Branca
* @since 2015-05-21
*/
class SimpleHttpClient
{
/**
* @var array
*/
protected $cookieJar = array();
/**
* @var string
*/
protected $baseUrl;
/**
* @var resource
*/
protected $ch;
/**
* Constructor
*
* @param string $baseUrl
*/
public function __construct($baseUrl)
{
$this->baseUrl = rtrim($baseUrl, '/') . '/';
$this->ch = curl_init();
}
/**
* Request (and store cookies from response in cookie jar)
*
* @param string $url
* @return SimpleXMLElement response
*/
public function request($url, $method = 'GET', array $data = array())
{
$url = ltrim($url, '/');
curl_setopt($this->ch, CURLOPT_URL, $this->baseUrl . $url);
curl_setopt($this->ch, CURLOPT_HEADER, false);
curl_setopt($this->ch, CURLOPT_COOKIE, $this->getCookieString());
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_HEADER, 1);
if ($method == 'POST') {
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
$response = curl_exec($this->ch);
$header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$this->addCookiesToJarFromHeader($header);
var_dump($body);
return simplexml_load_string($body);
}
/**
* Add cookie
*
* @param string $name
* @param string $value
*/
public function addCookie($name, $value)
{
$this->cookieJar[$name] = $value;
}
/**
* Extract cookies from header and add them to the cookie jar
*
* @param string $header
*/
protected function addCookiesToJarFromHeader($header)
{
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $header, $matches);
$cookies = array();
foreach ($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
$this->cookieJar = array_merge($this->cookieJar, $cookies);
}
/**
* Return all cookies as a string
*
* @return string
*/
protected function getCookieString()
{
$tmp = array();
foreach ($this->cookieJar as $name => $value) {
$tmp[] = "$name=$value";
}
return implode('; ', $tmp);
}
/**
* Destruct
*/
public function __destruct()
{
curl_close($this->ch);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment