Guest User

Untitled

a guest
Nov 14th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  3.  
  4. <module name="Bit68_Cart" setup_version="2.1.15"/>
  5.  
  6. </config>
  7.  
  8. <?xml version="1.0"?>
  9.  
  10. <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
  11.  
  12. <route url="/V1/cart/items" method="GET">
  13. <service class="Bit68CartApiCartMyCartInterface" method="getItems"/>
  14. <resources>
  15. <resource ref="self"/>
  16. </resources>
  17. </route>
  18.  
  19. </routes>
  20.  
  21. <?php
  22.  
  23. namespace Bit68CartApiCart;
  24.  
  25. interface MyCartInterface{
  26.  
  27. /**
  28. * @api
  29. * @param int $id
  30. * @return bool
  31. */
  32.  
  33. public function getItems();
  34.  
  35. }
  36.  
  37. ?>
  38.  
  39. <?php
  40.  
  41. namespace Bit68CartModelCart;
  42. use Bit68CartApiCartMyCartInterface;
  43. use MagentoCheckoutModelCartCartInterface;
  44. use MagentoCustomerModelSession;
  45.  
  46. class MyCart implements MyCartInterface{
  47. /**
  48. * @var MagentoCheckoutModelCartCartInterface
  49. */
  50. private $cartObj;
  51.  
  52. /**
  53. * @var MagentoCustomerModelSession
  54. */
  55. private $customerSession;
  56.  
  57. /**
  58. * @param MagentoCustomerModelSession $customerSession
  59. * @param MagentoCheckoutModelCart $cartObj
  60. */
  61.  
  62. public function __construct(MagentoCheckoutModelCart $cartObj,
  63. MagentoCustomerModelSession $customerSession){
  64. $this->cartObj = $cartObj;
  65. $this->customerSession = $customerSession;
  66. }
  67.  
  68. /**
  69. * @api
  70. * @return bool
  71. */
  72.  
  73. public function getItems(){
  74.  
  75. $customerId = $this->customerSession->isLoggedIn();
  76.  
  77. return $customerId;
  78. }
  79. }
  80.  
  81. ?>
  82.  
  83. <?php
  84.  
  85. session_start();
  86.  
  87. $host = 'http://domain.com/';
  88.  
  89.  
  90. if (!isset($_SESSION['access_token'])) {
  91. echo 'Authenticating...<br>';
  92. /*
  93. * authentication details of the customer
  94. */
  95. $username = 'customer_email';
  96. $password = 'password';
  97. $postData['username'] = $username;
  98. $postData['password'] = $password;
  99.  
  100. /*
  101. * init curl
  102. */
  103. $ch = curl_init();
  104. curl_setopt($ch, CURLOPT_URL, $host.'rest/V1/integration/customer/token');
  105. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  106. /*
  107. * set content type and length
  108. */
  109. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  110. 'Content-Type: application/json',
  111. 'Content-Length: '.strlen(json_encode($postData)),
  112. )
  113. );
  114. /*
  115. * setpost data
  116. */
  117. curl_setopt($ch, CURLOPT_POST, count($postData));
  118. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
  119. $output = curl_exec($ch);
  120. curl_close($ch);
  121. /*
  122. * access token in json format
  123. */
  124. echo $output;
  125. $_SESSION['access_token'] = $output;
  126. }
  127. if (isset($_SESSION['access_token'])) {
  128. /*
  129. * create headers for authorization
  130. */
  131. $headers = array(
  132. 'Authorization: Bearer '.json_decode($_SESSION['access_token']),
  133. );
  134. echo '<pre>';
  135. echo 'api call... with key: '.$_SESSION['access_token'].'<br><br><br>';
  136. $ch = curl_init();
  137. /*
  138. * set api resource url
  139. */
  140. $get_params = http_build_query($j);
  141. curl_setopt($ch, CURLOPT_URL, $host.'rest/V1/cart/items');
  142. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  143. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers
  144. );
  145. $output = curl_exec($ch);
  146.  
  147. curl_close($ch);
  148. echo '<br>';
  149. echo gettype($output);
  150. echo '<br>';
  151. echo $output;
  152. echo '<br>';
  153. echo "Today is " . date("Y-m-d") . "<br>";
  154. /*
  155. * 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
  156. */
  157. $test = json_decode(rtrim($output, '[]'));
  158. echo '
  159. =========================RESPONSE================================<br>
  160. ';
  161.  
  162. print_r($test);
  163. // echo($test->item_id);
  164. }
  165. exit(0);
  166.  
  167.  
  168. ?>
Add Comment
Please, Sign In to add comment