0

I'm trying to make my system send SMS usingSMSGatewayMe but i got Non-existent class: CI_ApiClient. When i try the Swagger example code it works, so maybe the error is from how i rewrite the code in codeigniter. Please help, i'm new to codeigniter :(

Swagger working code :

<?php
require 'vendor/autoload.php';

use SMSGatewayMe\Client\ApiClient;
use SMSGatewayMe\Client\Configuration;
use SMSGatewayMe\Client\Api\MessageApi;
use SMSGatewayMe\Client\Model\SendMessageRequest;

// Configure client
$config = Configuration::getDefaultConfiguration();
$config->setApiKey('Authorization', 'myApiKey');
$apiClient = new ApiClient($config);
$messageClient = new MessageApi($apiClient);

// Sending a SMS Message
$sendMessageRequest1 = new SendMessageRequest([
    'phoneNumber' => '12345667',
    'message' => 'test1',
    'deviceId' => 1
]);
$sendMessageRequest2 = new SendMessageRequest([
    'phoneNumber' => '07791064781',
    'message' => 'test2',
    'deviceId' => 2
]);
$sendMessages = $messageClient->sendMessages([
    $sendMessageRequest1
    //,$sendMessageRequest2
]);
print_r($sendMessages);

Sms.php Controller :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class Sms extends CI_Controller {

  public function index() {
    $this->load->library('session');
    $this->load->view('form_sms');
  }

  public function kirim_sms() {
    require 'vendor/SMSGatewayMe/autoload.php';

    $this->load->library('session');
    $this->load->library('CI_ApiClient');
    $this->load->library('CI_Configuration');
    $this->load->library('CI_MessageApi');
    $this->load->library('CI_SendMessageRequest');


    // Configure client
    $config = Configuration::getDefaultConfiguration();
    $config->setApiKey('Authorization', 'myApiKey');
    $apiClient = $this->load->library('CI_ApiClient', $config);
    $messageClient = new MessageApi($apiClient);

    $no_hp = $this->input->post('no_hp');

    $numbers = array();
    foreach ($no_hp as $key => $value) {
      if ($value != '') {
        array_push($numbers, $value);
      }
    }

    $deviceID = 1;
    $message = $this->input->post('isi_pesan');

    $result = $this->$messageClient->sendMessage($numbers, $message, $deviceID);
    $this->load->view('test_view', $result);
    if (count($result['response']['result']['success']) > 0) {
      $this->session->set_flashdata('message', '<div class="alert alert-success">Berhasil mengirim sms</div>');
    } else {
      $this->session->set_flashdata('message', '<div class="alert alert-danger">Gagal mengirim sms</div>');
    }

    redirect('sms','refresh');
  }
}

CI_ApiClient.php in libraries folder :

<?php
namespace SMSGatewayMe\Client;
defined('BASEPATH') OR exit('No direct script access allowed');

class CI_ApiClient
{

    public static $PATCH = "PATCH";
    public static $POST = "POST";
    public static $GET = "GET";
    public static $HEAD = "HEAD";
    public static $OPTIONS = "OPTIONS";
    public static $PUT = "PUT";
    public static $DELETE = "DELETE";

    /**
     * Configuration
     * @var Configuration
     */
    protected $config;

    /**
     * Object Serializer
     * @var ObjectSerializer
     */
    protected $serializer;

    /**
     * Constructor of the class
     * @param Configuration $config config for this ApiClient
     */
    public function __construct(Configuration $config = null)
    {
        if ($config == null) {
            $config = Configuration::getDefaultConfiguration();
        }

        $this->config = $config;
        $this->serializer = new ObjectSerializer();
    }
...
  • I think you've made things more complicated by using the `CI_` prefix. [The docs are good and describe clearly how to add your own library](https://codeigniter.com/user_guide/general/creating_libraries.html). 1) That `CI_` prefix is usually only used when your are overriding a native CI library. Maybe that is causing problems? 2) If your library was overriding or extending a native CI one you would normally load it without the `CI_` prefix, ie `$this->load->library('apiclient');`. 3) Just to be sure, your file is in `application/libraries/`? – Don't Panic Nov 15 '19 at 06:48
  • 1) I had tried not using CI_ prefix but still got the same problem 2)the library is from third party, so i put the full library in application/thirdparty/ 3) yes I copy the ApiClient.php files from the full library and then put it in application/libraries/. In other solutions is saying to change something in config/autoload files, but when i change those files it gives other errors.. – Afifah N. Nov 15 '19 at 07:25
  • OK, that's new info. I don't have experience with that but [the docs describe how to use code in the third party dir](https://codeigniter.com/user_guide/libraries/loader.html). Personally I would get rid of the `CI_` prefix just to make things simple and remove one more potential problem. – Don't Panic Nov 15 '19 at 08:01
  • do you use composer for such things ? - since you have a vendor folder, it looks like you do isn't it ? – Atural Nov 15 '19 at 09:28
  • yes the library required composer to be executed – Afifah N. Nov 15 '19 at 14:00
  • I solved this. The problem is that I install the library without using the project composer (source : https://github.com/ramsey/uuid/issues/54). After that i need to integrate project controller to library (source : https://stackoverflow.com/questions/13741917/how-to-use-composer-packages-in-codeigniter , https://stackoverflow.com/questions/50193162/cannot-find-class-in-vendor-folder-codeigniter) – Afifah N. Nov 17 '19 at 09:42
  • Thanks for your help Don't Panic and sintakonte :D – Afifah N. Nov 17 '19 at 09:43

1 Answers1

0

Remove namespace SMSGatewayMe\Client;