Tag Archives: API

Magento 2 API Add Product To Cart

The following code will add product to a cart/quote for a logged-in customer.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/customer/token";
 $data = array("username" => "Test.User1521623008@phpcodez.com", "password" => "phpcodezpwd");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);

$quoteUrl =$magentoURL.'index.php/rest/V1/carts/mine';

$chQuote = curl_init($quoteUrl);
 curl_setopt($chQuote, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 curl_setopt($chQuote, CURLOPT_RETURNTRANSFER, true);
 $quote = json_decode(curl_exec($chQuote));
 
 $cartData = [
 'cartItem' => [
 "quote_id" => $quote->id,
 "sku" => "152110360696",
 "qty" => 2
 ]
 ];

$requestUrl =$magentoURL.'index.php/rest/V1/carts/mine/items';

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($cartData));
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);

?>
Share

Magento 2 API Create Cart Customer

The following code will create a cart/quote for a logged-in customer.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/customer/token";
 $data = array("username" => "Test.User1521623008@phpcodez.com", "password" => "phpcodez&");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);
 $headers = array("Authorization: Bearer ".json_decode($token));
 $requestUrl =$magentoURL.'index.php/rest/V1/carts/mine';
 $ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $result = curl_exec($ch);
 $result= json_decode($result);
 echo "<pre>";print_r($result);
 ?>
Share

Magento 2 API Create Customer

Here I am creating a customer using REST API

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "APIUSER", "password" => "APIUSERPWD");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);

$customerData = [
 'customer' => [
 "email" => 'Test.User'.time().'@phpcodez.com',
 "firstname" => "Brand",
 "lastname" => "Muscle",
 "storeId" => 1,
 "websiteId" => 1
 ],
 "password" => "PHPCodez1&"
 ];

$ch = curl_init($magentoURL."index.php/rest/V1/customers");
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

$result = curl_exec($ch);

$result = json_decode($result, 1);
 echo '<pre>';print_r($result);
?>
Share

Magento 2 REST API

Magento 2 API framework allows developers to create new services for communicating with Magento 2 stores.

Here I will use this Token based REST API to get all the customers from a Magento 2 store. So before each API call we should get the token from Magento and we should pass this token in the header of every request.

Also to achive this you need to create a new admin user with necessary access to the system.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "APIUSER", "password" => "APIUSERWD");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);

//decoding generated token and saving it in a variable
 $token= json_decode($token);

//******************************************//

//Using above token into header
 $headers = array("Authorization: Bearer ".$token);

//API URL to get all Magento 2 modules
 $requestUrl =$magentoURL.'index.php/rest/V1/customers/search?searchCriteria[sortOrders][0][field]=email&searchCriteria[sortOrders][0][direction]=asc';

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $result = curl_exec($ch);

//decoding result
 $result= json_decode($result);

//printing result
 echo "<pre>";print_r($result);

?>
Share

Magento 2 Create API

API stands for Application Programming Interface to allow you access the data from an application. API can be called as a middleman between a programmer and an application. When the programmer calls for a request via the middleman, if the request is approved, the right data will be turned back.

Follow the below steps to create APIs

Web API configuration

In webapi.xml we are configuring access rights and API Interface that specified method will use.

File : app/code/PHPCodez/First/etc/webapi.xml Continue reading Magento 2 Create API

Share

Difference Between REST and SOAP

Simple Object Access Protocol (SOAP) standard an XML language defining a message architecture and message formats, is used by Web services it contain a description of the operations. WSDL is an XML-based language for describing Web services and how to access them. will run on SMTP,HTTP,FTP etc. Requires middleware support, well defined mechanisam to define services like WSDL+XSD, WS-Policy SOAP will return XML based data

REST Representational State Transfer (RESTful) web services. they are second generation Web Services. RESTful web services, communicate via HTTP than SOAP-based services and do not require XML messages or WSDL service-API definitions. for REST no middleware is required only HTTP support is needed.WADL Standard, REST can return XML, plain text, JSON, HTML etc

REST is almost always going to be faster. The main advantage of SOAP is that it provides a mechanism for services to describe themselves to clients, and to advertise their existence.

REST is much more lightweight and can be implemented using almost any tool, leading to lower bandwidth and shorter learning curve. However, the clients have to know what to send and what to expect.

In general, When you’re publishing an API to the outside world that is either complex or likely to change, SOAP will be more useful. Other than that, REST is usually the better option.

Share

Script to automatically delete a user – VBulletin

1)Create a file with desired name (let it be vb_delete.php) with the following code

require_once(‘class.forumops.php’);
$clientdata = unserialize($_POST[‘post’]);
$forum = new ForumOps();
$errmsg = $forum->delete_user($clientdata[‘username’]);
if ($errmsg) echo $errmsg;
else echo “User deleted successfully.
n”;

NOTE :You can download the script using the url http://wp.me/p1aZaf-1d

2)Upload the file to the server the vBulletin installed

3)Paste the below given code , immediately after the code that update the user details .

$clientdata = array( ‘username’ => $username);
$url=”http://www.example.com/forum/vb_delete.php”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, array(“post” => serialize($clientdata)));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
echo $store;
curl_close ($ch);
exit;*/
// http://www.example.com/forum/ is the location where vbulletin is installed

Share