<?php
class Api {
    public $api_key = '';

     /**
     *
     * Add Order
     *
     */
    public function add_order($data) { 
        $post = array_merge(array('key' => $this->api_key, 'action' => 'add'), $data);
        $result = $this->fetch($post);
        return json_decode($result);
    }

    /**
     *
     * Order status
     *
     */
    public function status($order_id) { 
        $result = $this->fetch(array(
            'key'    => $this->api_key,
            'action' => 'status',
            'order'  => $order_id
        ));
        return json_decode($result);
    }

    //get orders list
    public function orders() { 
        $result = $this->fetch(array(
            'key'    => $this->api_key,
            'action' => 'orders',
        ));
        return json_decode($result);
    }

    public function services() { 
        $result = $this->fetch(array(
            'key'     => $this->api_key,
            'action'  => 'services',
        ));
        return json_decode($result);
    }

    /**
     *
     * Balance
     *
     */
    public function balance() { 
        $result = $this->fetch(array(
            'key'     => $this->api_key,
            'action'  => 'balance',
        ));
        return json_decode($result);
    }

    private function fetch(array $data)
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => 'http://localhost/smm/api/v1',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => $data,

        ));

        $response = curl_exec($curl);

        curl_close($curl);
        return json_encode($response);
    }
    
}

$api = new Api();

// add order
$order = $api->add_order(array('service' => 2, 'link' => 'http://example.com/test', 'quantity' => 1)); # Default

# return all services
$services = $api->services(); 

# return user balance
$balance = $api->balance(); 

# return order Status
$status = $api->status(23); # Default

# return orders list
$orders = $api->orders(); 
