Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0"?>
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
- <module name="Bit68_Cart" setup_version="2.1.15"/>
- </config>
- <?xml version="1.0"?>
- <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
- <route url="/V1/cart/items" method="GET">
- <service class="Bit68CartApiCartMyCartInterface" method="getItems"/>
- <resources>
- <resource ref="self"/>
- </resources>
- </route>
- </routes>
- <?php
- namespace Bit68CartApiCart;
- interface MyCartInterface{
- /**
- * @api
- * @param int $id
- * @return bool
- */
- public function getItems();
- }
- ?>
- <?php
- namespace Bit68CartModelCart;
- use Bit68CartApiCartMyCartInterface;
- use MagentoCheckoutModelCartCartInterface;
- use MagentoCustomerModelSession;
- class MyCart implements MyCartInterface{
- /**
- * @var MagentoCheckoutModelCartCartInterface
- */
- private $cartObj;
- /**
- * @var MagentoCustomerModelSession
- */
- private $customerSession;
- /**
- * @param MagentoCustomerModelSession $customerSession
- * @param MagentoCheckoutModelCart $cartObj
- */
- public function __construct(MagentoCheckoutModelCart $cartObj,
- MagentoCustomerModelSession $customerSession){
- $this->cartObj = $cartObj;
- $this->customerSession = $customerSession;
- }
- /**
- * @api
- * @return bool
- */
- public function getItems(){
- $customerId = $this->customerSession->isLoggedIn();
- return $customerId;
- }
- }
- ?>
- <?php
- session_start();
- $host = 'http://domain.com/';
- if (!isset($_SESSION['access_token'])) {
- echo 'Authenticating...<br>';
- /*
- * authentication details of the customer
- */
- $username = 'customer_email';
- $password = 'password';
- $postData['username'] = $username;
- $postData['password'] = $password;
- /*
- * init curl
- */
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $host.'rest/V1/integration/customer/token');
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- /*
- * set content type and length
- */
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: '.strlen(json_encode($postData)),
- )
- );
- /*
- * setpost data
- */
- curl_setopt($ch, CURLOPT_POST, count($postData));
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
- $output = curl_exec($ch);
- curl_close($ch);
- /*
- * access token in json format
- */
- echo $output;
- $_SESSION['access_token'] = $output;
- }
- if (isset($_SESSION['access_token'])) {
- /*
- * create headers for authorization
- */
- $headers = array(
- 'Authorization: Bearer '.json_decode($_SESSION['access_token']),
- );
- echo '<pre>';
- echo 'api call... with key: '.$_SESSION['access_token'].'<br><br><br>';
- $ch = curl_init();
- /*
- * set api resource url
- */
- $get_params = http_build_query($j);
- curl_setopt($ch, CURLOPT_URL, $host.'rest/V1/cart/items');
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers
- );
- $output = curl_exec($ch);
- curl_close($ch);
- echo '<br>';
- echo gettype($output);
- echo '<br>';
- echo $output;
- echo '<br>';
- echo "Today is " . date("Y-m-d") . "<br>";
- /*
- * json response need to rtrim with [], some times it is appended to the respose so the json becomes invalid so need to rtrim the response
- */
- $test = json_decode(rtrim($output, '[]'));
- echo '
- =========================RESPONSE================================<br>
- ';
- print_r($test);
- // echo($test->item_id);
- }
- exit(0);
- ?>
Add Comment
Please, Sign In to add comment