I have a problem with creating my custom api.
I've created directory app/code/Openc/GCApi
with the following files:
- app/code/Openc/GCApi/etc/module.xml
<?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="Openc_GCApi" setup_version="1.0.0" />
</config>
- app/code/Openc/GCApi/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Openc_GCApi',
__DIR__
);
- app/code/Openc/GCApi/etc/webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<route url="/V1/gcapiapi/:name" method="GET">
<service class="Openc\GCApiApi\Api\GcapiapiInterface" method="name" />
<resources>
<resource ref="self"/>
</resources>
</route>
- app/code/Openc/GCApi/etc/di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Openc\GCApiApi\Api\GcapiapiInterface" type="Openc\GCApiApi\Model\Gcapiapi"/>
</config>
- app/code/Openc/GCApi/Api/GcapiapiInterface.php
<?php
namespace Openc\GCApi\Api;
interface GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @api
* @return string Greeting message with users name.
*/
public function name(string $name);
}
- app/code/Openc/GCApi/Model/Gcapiapi.php
<?php
namespace Openc\GCApiApi\Model;
use Openc\GCApi\Api\GcapiapiInterface;
class Gcapiapi implements GcapiapiInterface
{
/**
* Returns greeting message to user
*
* @api
* @param string $name Users name.
* @return string Greeting message with users name.
*/
public function name(string $name) {
return "Hello ".$name;
}
}
Here is the file for testing my custom API:
<?php
define('BASEURL','[base url]');
$apiUser = 'apicustomer';
$apiPass = 'admin123';
$apiUrl = BASEURL.'index.php/rest/V1/integration/admin/token';
$data = array("username" => $apiUser, "password" => $apiPass);
$data_string = json_encode($data);
try{
$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);
$token = json_decode($token);
if(isset($token->message)){
echo $token->message;
}else{
$key = $token;
}
}catch(Exception $e){
echo 'Error: '.$e->getMessage();
}
$headers = array("Authorization: Bearer $key");
$requestUrl = BASEURL.'index.php/rest/V1/gcapiapi/testname';
$ch = curl_init();
try{
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$result = json_decode($result);
if(isset($result->message)){
echo $result->message;
}else{
print_r($result);
}
}catch(Exception $e){
echo 'Error: '.$e->getMessage();
}
And here is the error which I'm getting:
Class Openc\GCApiApi\Api\GcapiapiInterface
does not exist.