I need to get current customer cart items using rest API, so I've created a Cart module and
Here is my interface:
<?php
namespace Bit68\Cart\Api\Cart;
interface MyCartInterface{
/**
* @api
* @return array
*/
public function getItems();
}
?>
here is my class that implements the interface: <?php
namespace Bit68\Cart\Model\Cart;
use Bit68\Cart\Api\Cart\MyCartInterface;
use \Magento\Checkout\Model\Cart\CartInterface;
class MyCart implements MyCartInterface{
/**
* @var Magento\Checkout\Model\Cart\CartInterface
*/
private $cartObj;
public function __construct(\Magento\Checkout\Model\Cart $cartObj){
$this->cartObj = $cartObj;
}
/**
* @api
* @return array
*/
public function getItems(){
$items = $this->cartObj->getQuote()->getAllItems();
return $items;
}
}
?>
Here is webapi.xml:
<?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="\Bit68\Cart\Api\Cart\MyCartInterface" method="getItems"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
and that's where I call my api:
<?php
session_start();
/*
* base url of the magento host
*/
$host = 'http://127.0.0.1/yourParts/';
//unset($_SESSION['access_token']);
if (!isset($_SESSION['access_token'])) {
echo 'Authenticating...<br>';
/*
* authentication details of the customer
*/
$username = 'mail';
$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)),
)
);
/*
* set post 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
*/
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 response 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);
?>
and that is my module tree structure