I have a fresh LUMA sample data site set up.
I set up an integration user so that I could access the API with the proper security keys.
I saw in the Imagine 2016 video that there are some out of the box APIs to use and Swagger gives some technical details on constructs.
But I am a beginner at this.
I need some "how to" steps, in PHP code, for doing basic connectivity, like "Request all Items from the Item Table". And if I need to on the back end enable certain functions by editing some files, I need those steps outlined as well.
I have been unable to find this anywhere. All snippets and documentation assume that I know more than I do.
***************************************************************
Many many thanks to http://blog.i13websolution.com/
and their Content
MAGENTO 2 REST API EXAMPLE
I was able to make minor edits to their example code in order to conduct a basic conversation with my sample data site. Since this is working with only a back end user name and password, the integration user and generated security keys are not involved. Hope to learn more about that concept soon.
<?php
//Authentication rest API magento2.Please change url accordingly your url
$adminUrl='mywebsite.com/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "myusername", "password" => "mypassword");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
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);
$token= json_decode($token);
//Use above token into header
$headers = array("Authorization: Bearer $token");
//Please note 24-MB01 is sku
$requestUrl='mywebsite.com/index.php/rest/V1/products/24-MB01';
$ch = curl_init();
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result= json_decode($result);
print_r($result);
i've found very usefull for this https://www.getpostman.com/ that among other stuff ( like setting up the authorization need for magento) it shows you also the code generated for the call you are testing
and example
<?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "http://m2-playground.dev/rest/V1/products/MH07-M-Green", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "PUT", CURLOPT_POSTFIELDS => "{\n \"product\": {\n \"sku\": \"MH07-M-Green\",\n \"name\": \"Hero Hoodie-M-Green new\",\n \"attributeSetId\": 9\n },\n \"saveOptions\": true\n}", CURLOPT_HTTPHEADER => array( "authorization: OAuth oauth_consumer_key=\"33mun8ejynbmsii298tcd1r1g521k6gp\",oauth_token=\"vmm8mmo0oc52ysgbg68wqp22qtmp4hbn\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1466211088\",oauth_nonce=\"LUBaL2\",oauth_version=\"1.0\",oauth_signature=\"WQrhwzcV4wGr67S%2FWX7Z2usjvLM%3D\"", "cache-control: no-cache", "content-type: application/json", "postman-token: 6cf9c3e6-8041-e09f-5807-62c4a8fa98e9" ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; }