2

I have a problem with creating my custom api. I've created directory app/code/Openc/GCApi with the following files:

  1. 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>
  1. app/code/Openc/GCApi/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
       \Magento\Framework\Component\ComponentRegistrar::MODULE,
       'Openc_GCApi',
       __DIR__
);
  1. 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>
  1. 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>
  1. 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);
     }
  1. 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.

1
  • @ Alex, Have you resolved the problem ?
    – MGento
    Commented Oct 31, 2017 at 9:42

1 Answer 1

1

Looks like you have some places where your namespaces are mismatched. You're using GCApiApi in your namespace/xml, instead of GCApi. I assume that's not intentional? Try correcting this.

2
  • Yea, Joe is right.
    – MGento
    Commented Oct 30, 2017 at 17:16
  • @ Alex, Open your file app/code/Openc/GCApi/Model/Gcapiapi.php and update the line, namespace Openc\GCApiApi\Model; to namespace Openc\GCApi\Model;
    – MGento
    Commented Oct 30, 2017 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.