Jump to content
BlackDream

API - Create Users/MAGS - View Offline/Online Streams - Start/Stop Streams - Add/Remove Credits and more

Recommended Posts

Dear Customers,

In this tutorial we are going to learn how we can use the API that was released in v2.3.x. To successfully use the API, you have to import your IP first into the whitelist. You can do that in General Settings -> API Settings.

1.png

Creating New Line

To create a New line, we will call the following URL.

http://dns:port/api.php?action=user&sub=create

The above URL, accepts the POST action, and to create a new line we will have to specify some arguments in an array called user_data

Example Code:

 

<?php

$panel_url = 'http://DNS:PORT/';
$username = 'test_username';
$password = 'test_password';
$max_connections = 1;
$reseller = 1;
$bouquet_ids = array(
    1,
    2,
    3 );
$expire_date = strtotime( "+1 month" );

###############################################################################
$post_data = array( 'user_data' => array(
        'username' => $username,
        'password' => $password,
        'max_connections' => $max_connections,
        'is_restreamer' => $reseller,
        'exp_date' => $expire_date,
        'bouquet' => json_encode( $bouquet_ids ) ) );

$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );

$context = stream_context_create( $opts );
$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=user&sub=create", false, $context ) );

?>

 

If you leave the username and/or password elements empty, the system will generate random characters for these 2 fields. In addition, leaving any field empty, api will use the default value for this field according to SQL Database. So theoretically, you can make a line with just Calling the Above URL, without any parameter, however the created user, wont have any bouquets inside.

You can call any other element that is in the database like 

  • member_id
  • admin_enabled
  • enabled
  • allowed_ips
  • allowed_ua
  • force_server_id
  • is_isplock
  • admin_notes
  • and so on...

If you want to set the allowed_ips/allowed_ua since this is a JSON Encoded format you can do it like bouquet in the above Code.

The API will return a simple json encoded string that upon decoding will contain the element result which returns true/false.

Example(API Success)

{"result":true,"created_id":14838,"username":"d4PSc5uCqF","password":"2ZiuRRZk4b"}

The API returned as the username/password of the NEW line, as well as the ID so we can use it wherever we want.

 

Example(API Failed)

{"result":false,"error":"EXISTS"}
{"result":false,"error":"PARAMETER ERROR"}
  • EXISTS 
    The Username you specified already exists in the database
  • PARAMETER ERROR
    You wrote invalid characters in your user_data array

Editing Line

To procedure to edit a line is very similar to the above. The URL we will call this time is

http://dns:port/api.php?action=user&sub=edit

REQUIRED PARAMETERS

  • username
  • password

OPTIONAL PARAMETERS

  • user_data array

For example if we want to Edit the Expire Date, make the line restreamer and adjusting the max connections to 10 we will do it like this:

<?php

$panel_url = 'http://DNS:PORT/';
$username = 'test_username';
$password = 'test_password';
$max_connections = 10;
$reseller = 1;
$expire_date = strtotime( "+1 month" ); //from the time now, not from line's expire date.

###############################################################################
$post_data = array(
    'username' => $username,
    'password' => $password,
    'user_data' => array(
        'max_connections' => $max_connections,
        'is_restreamer' => $reseller,
        'exp_date' => $expire_date ) );

$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );

$context = stream_context_create( $opts );
$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=user&sub=edit", false, $context ) );

?>

 

In the above example, we will edit the max_connections, is_restreamer and exp_date for our line with username test_username, and password test_password that already exists in our database.

Example(API Success)

{"result":true}

 

Example(API Failed)

{"result":false,"error":"NOT EXISTS"}
{"result":false,"error":"PARAMETER ERROR"}
{"result":false,"error":"PARAMETER ERROR (user\/pass)"}
  • NOT EXISTS 
    The Username / Password you specified are not exists in the database
  • PARAMETER ERROR
    You wrote invalid characters in your user_data array
  • PARAMETER ERROR (user/pass)
    The Username OR/AND Password elements are missing

 

View Line Information

With this API call, we will get all the information available about our line including the active connections.

The URL we will call this time is

http://dns:port/api.php?action=user&sub=info

REQUIRED PARAMETERS

  • username
  • password

It will return a JSON Encoded string, with all information that you might want.

Example Code:

$panel_url = 'http://DNS:PORT/';
$username = "username";
$password = "passwrd";


###############################################################################
$post_data = array( 'username' => $username, 'password' => $password );
$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );

$context = stream_context_create( $opts );
$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=user&sub=info", false, $context ), true );

if ( $api_result['result'] )
{
    echo "Active Connections (Now): " . $api_result['user_info']['active_cons'];
    echo "\nCurrent Expire Date: " . (( empty( $api_result['user_info']['exp_date'] ) ) ? 'Unlimited' : strtotime( $api_result['user_info']['exp_date'] ));
    echo "\nMax Connections: " . $api_result['user_info']['max_connections'];
    echo "\nAvailable Channel IDs: " . implode( ',', $api_result['user_info']['channel_ids'] );
}
else
    echo 'FAILED';

 

Create/Edit & View Information on MAG Devices

The procedure is almost the same as creating/editing & view info on a user line. The only difference is that instead of calling the  user action, we will the stb action. Of course instead of username/password we will just have the mac parameter.

 

Example API Call To Create New MAG

http://dns:port/api.php?action=stb&sub=create

REQUIRED PARAMETERS

  • user_data[mac]
<?php

$panel_url = 'http://DNS:PORT/';
$mac = '00:1A:79:79:79:79';
$bouquet_ids = array(
    1,
    2,
    3 );
$expire_date = strtotime( "+1 month" );

###############################################################################
$post_data = array( 'user_data' => array(
        'mac' => $mac,
        'exp_date' => $expire_date,
        'bouquet' => json_encode( $bouquet_ids ) ) );

$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );

$context = stream_context_create( $opts );
$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=stb&sub=create", false, $context ) );
print_r($api_result);

?>

Example(API Success)

{"result":true}

 

Example(API Failed)

{"result":false,"error":"NOT EXISTS"}
{"result":false,"error":"PARAMETER ERROR"}
{"result":false,"error":"PARAMETER ERROR (mac)"}
  • EXISTS 
    The MAC already exists in the database
  • PARAMETER ERROR
    You wrote invalid characters in your user_data array
  • PARAMETER ERROR (mac)
    The MAC Parameter was missing

 

Example API Call To Edit a MAG Device

http://dns:port/api.php?action=stb&sub=edit

REQUIRED PARAMETERS

  • mac
<?php

$panel_url = 'http://DNS:PORT/';
$mac = '00:1A:79:79:79:79';
$bouquet_ids = array(
    1,
    2,
    3 );
$expire_date = strtotime( "+1 month" );

###############################################################################
$post_data = array( 'mac' => $mac, 'user_data' => array( 'exp_date' => $expire_date, 'bouquet' => json_encode( $bouquet_ids ) ) );

$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );

$context = stream_context_create( $opts );
$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=stb&sub=edit", false, $context ) );
?>

Example(API Success)

{"result":true}

 

Example(API Failed)

{"result":false}
{"result":false,"error":"PARAMETER ERROR"}
{"result":false,"error":"PARAMETER ERROR (mac)"}
  • no error
    The updated information, are the same as in the database, so no action taken.
  • PARAMETER ERROR
    You wrote invalid characters in your user_data array
  • PARAMETER ERROR (mac)
    The MAC Parameter was missing

 

View Information MAG Device

http://dns:port/api.php?action=stb&sub=info

REQUIRED PARAMETERS

  • mac

It will return a JSON Encoded string, with all information that you might want.

Example Code:

<?php

$panel_url = 'http://DNS:PORT/';
$mac = '00:1A:79:79:79:79';


###############################################################################
$post_data = array( 'mac' => $mac);
$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );

$context = stream_context_create( $opts );
$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=stb&sub=info", false, $context ), true );


if ( $api_result['result'] )
{
    echo "Active Connections (Now): " . $api_result['user_info']['active_cons'];
    echo "\nCurrent Expire Date: " . ( ( empty( $api_result['user_info']['exp_date'] ) ) ? 'Unlimited' : strtotime( $api_result['user_info']['exp_date'] ) );
    echo "\nMax Connections: " . $api_result['user_info']['max_connections'];
    echo "\nAvailable Channel IDs: " . implode( ',', $api_result['user_info']['channel_ids'] );
}
else
    echo 'FAILED';

?>

Share this post


Link to post
Share on other sites

API has been updated and few bugs regarding bouquet handling are now fixed.

Also, here are few other requests you can make to the API

 

  • View Server List
    Perform this request, to view all your servers, main & load balancers including their status

    http://your_dns:port/api.php?action=server&sub=list
     
  • View Online Streams
    Perform this request, to view only the online Live Streams

    http://your_dns:port/api.php?action=stream&sub=online
     
  • View Offline Streams
    Perform this request, to view only the Offline Live Streams

    http://your_dns:port/api.php?action=server&sub=offline
     
  • Start/Restart A Stream
    Perform this request, to start or restart a Live Stream. The last argument is an array in which you can specify the stream ids. It works with POST method as well.

    http://your_dns:port/api.php?action=stream&sub=start&stream_ids[]=1
     
  • Stop A Stream
    Perform this request, to stop a Live Stream. The last argument is an array in which you can specify the stream ids. It works with POST method as well.

    http://your_dns:port/api.php?action=streamsub=stop&stream_ids[]=1

Share this post


Link to post
Share on other sites

API has been updated to add support for registered users credit handling

 

  • View Registered Users list
    Perform this request, to view all your registered users along with their email, username, credits, member group etc

    http://your_dns:port/api.php?action=reg_user&sub=list
     
  • Add/Remove Credits
    Perform this request, to add or remove credits from a user

    http://your_dns:port/api.php?action=reg_user&sub=credits&amount=X&id=X
     

    The amount can be either positive or negative number. If you are trying to remove an amount of credits higher than the current credits of the user you will receive an error message ( NOT ENOUGH CREDITS )

    The id, is the id of the user. You can also perform the same request using username parameter.

Share this post


Link to post
Share on other sites

Dear Sir,

I contact you to ask to help me because i get the folowing error while using the API.

This is the error I got :

[16-Jun-2017 19:09:49 Europe/London] PHP Warning: file_get_contents(http://my-dns.com:8000/api.php?action=user&amp;sub=create): failed to open stream: Connection refused in /home/sight/mytv/proiptv/index.php on line 26

when using this script to add new MAG user

<?php
 
$panel_url = 'http://my-dns:8000/';
$mac = '00:1A:79:79:79:79';
$bouquet_ids = 2;
$expire_date = strtotime( "+1 day" );
$max_connections = 1;
$reseller = 180;
 
###############################################################################
$post_data = array( 'user_data' => array(
        'mac_address' => $mac,
        'expire_date' => $expire_date,
        'bouquet' =>  $bouquet_ids ,
        'max_cons' => $max_connections,
        'reseller' => $reseller
            ) 
        );
 
$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );
 
$context = stream_context_create( $opts );
$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=user&sub=create", false, $context ) );
print_r($api_result);
 
?>
 
I inform you that i want to execute the code from website server which have IP (xx.xx.0.134) different than IPTV server (pro.leaderiptv.com).
 
i inform you that i added the IP (xx.xx.0.134) in the Xtream Panel API setting as follow :
autorised IP : xx.xx.0.134
Password API key : 7xmMWYTufgds4343T48YXaz8
I still always getting error.

 

Please help.

Thank you

Share this post


Link to post
Share on other sites

In localhost everything works fine, but on my hosting i get error "  i get failed to open stream ", any advice?

 

$panel_url = 'http:/xxxxx/';
$username = "axxx";
$password = "axxxx";


###############################################################################
$post_data = array( 'username' => $username, 'password' => $password );
$opts = array( 'http' => array(
        'method' => 'POST',
        'header' => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query( $post_data ) ) );

$context = stream_context_create( $opts );

$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=user&sub=info", false, $context ), true );

var_dump ($api_result);

Share this post


Link to post
Share on other sites

what i did was created a apache server on port 80.. i added my script there... a script was created for us were we just enter a username and password and then click go .. it creates a account for the re-seller and also removes 1 credit from their account

Share this post


Link to post
Share on other sites

How can i stop a stream via api?

 

##### Dont work, only "{"result":true}":

  • Start/Restart A StreamPerform this request, to start or restart a Live Stream. The last argument is an array in which you can specify the stream ids. It works with POST method as well.http://your_dns:port/api.php?action=stream&sub=start&stream_ids[]=1 
  • Stop A StreamPerform this request, to stop a Live Stream. The last argument is an array in which you can specify the stream ids. It works with POST method as well.http://your_dns:port/api.php?action=streamsub=stop&stream_ids[]=1

Share this post


Link to post
Share on other sites
On 6/27/2017 at 7:17 AM, Spyer said:

How can i stop a stream via api?

 

##### Dont work, only "{"result":true}":

  • Start/Restart A StreamPerform this request, to start or restart a Live Stream. The last argument is an array in which you can specify the stream ids. It works with POST method as well.http://your_dns:port/api.php?action=stream&sub=start&stream_ids[]=1 
  • Stop A StreamPerform this request, to stop a Live Stream. The last argument is an array in which you can specify the stream ids. It works with POST method as well.http://your_dns:port/api.php?action=streamsub=stop&stream_ids[]=1

Exactly like it says on there... where it says the number 1 you replace it with the stream you want... i did notice however if you use a load balancer it will only stop the stream on the main server

Share this post


Link to post
Share on other sites

Hi,

my mainserver have no streams. Only my the LB-Servers.

How can I restart/stop the streams on the LB ?
=Stop&stream_id[]=1&server_id=1 does not work

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×