Last active
July 7, 2021 11:59
-
-
Save fbrnc/bdade1df328c3f766a3c to your computer and use it in GitHub Desktop.
Magento XML Connect Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Initialize | |
$app = new SimpleHttpClient('http://www.demo.local/xmlconnect/'); | |
$app->addCookie('app_code', 'INSERT APP CODE HERE'); | |
$app->addCookie('screen_size', '600x400'); | |
// get products in category 3 | |
echo "\n[REQUEST] Fetching product catalog...\n"; | |
$response = $app->request('catalog/category/id/3/offset/0/count/100'); | |
if ((string)$response->status == 'error') { | |
echo "Error: " . (string)$response->text . "\n"; | |
exit(1); | |
} | |
$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 cart content and verify the item we just added is there | |
echo "\n[REQUEST] Fetching cart content...\n"; | |
$cart = $app->request('cart'); | |
if ((int)$cart->products->item[0]->entity_id != $productId) { | |
echo "Could not find product in cart"; | |
exit(1); | |
} | |
echo "Verified that product is in cart now.\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' => 'San Francisco', | |
'country_id' => 'US', | |
'region_id' => '12', // California | |
'postcode' => '94015', | |
'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"; | |
// get shipping methods | |
echo "\n[REQUEST] Fetching available shipping methods...\n"; | |
$response = $app->request('checkout/shippingMethodsList'); | |
$shippingMethods = array(); | |
foreach ($response->method->rates->rate as $rate) { | |
$shippingMethods[] = (string)$rate['code']; | |
echo " - {$rate['code']}\n"; | |
} | |
// 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'))); | |
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); | |
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); | |
} | |
} |
Thank you for your video tutorial.
I am confused that why XML Connect should be in php language while we want to use it in mobile applications?
How to use this php example in Android Studio?
Do I have to setup a second php server and upload this php code to the server then try to communicate from Java to php server?
Is there any documentation on how to implement a simple data retrieval (e.g. new orders coming) app for android?
@JToward, i was wonder if you manage to progress further after posting the above questions. Thanks.
/Tommy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for your video tutorial.
I am confused that why XML Connect should be in php language while we want to use it in mobile applications?
How to use this php example in Android Studio?
Do I have to setup a second php server and upload this php code to the server then try to communicate from Java to php server?
Is there any documentation on how to implement a simple data retrieval (e.g. new orders coming) app for android?