API IN CODEIGNITOR

Standard

api.php controller

 

<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);class Api extends CI_Controller {function __construct() { parent::__construct();
date_default_timezone_set(‘Australia/canberra’);  $militime =round(microtime(true) * 1000); $datetime =date(‘Y-m-d h:i:s’); define(‘militime’, $militime); define(‘datetime’, $datetime); }
public function login(){ $email = $this->input->post(’email’); $name = $this->input->post(‘name’); $image = $this->input->post(‘image’); $contact_no = $this->input->post(‘contact_no’); $device_token = $this->input->post(‘device_token’); $device_id = $this->input->post(‘device_id’); $device_type = $this->input->post(‘device_type’);    $type = $this->input->post(‘social_login_type’);
if($device_type == ‘iOS’ && strlen($device_id) === strlen($device_token))    {         $device_token = ”;    }
$version_code = $this->input->post(‘version_code’);
$auth_key = $this->rand_string(40);
$final_output = array();
if(!empty($email))    {       $get_version = $this->common_model->common_getRow(‘app_version’,array(‘version_id’=>1));         if(($version_code < $get_version->android_min_version && $device_type == ‘android’) || ($version_code < $get_version->ios_min_version && $device_type == ‘iOS’))     {     if($device_type == ‘android’)     {     $msg = ‘This version of <b>ATMC</b> is became to obsolete. Please go to Google Play to download the latest version’;      }     else     {     $msg = ‘This version of ATMC is became to obsolete. Please go to App Store to download the latest version’;     }
$final_output[‘status’] = ‘1001’; $final_output[‘message’] = $msg; header(“content-type: application/json”);     echo json_encode($final_output);exit;     }     else     {  $check_email = $this->common_model->common_getRow(‘students’,array(’email’=>$email)); if(!empty($check_email)) {     $update_value = $this->common_model->updateData(‘students’,array(‘update_at’=>militime,’token’=>$auth_key,’device_id’=>$device_id,’device_token’=>$device_token,’device_type’=>$device_type,’image’=>$image,’name’=>$name,’social_login_type’=>$type,’contact_no’=>$contact_no,),array(’email’=>$email));
$student_id = $check_email->student_id;
$query = $this->db->query(“UPDATE `students` SET `device_token` = ” WHERE `device_id` = ‘$device_id’ AND `student_id` != ‘$student_id'”);

$data_array = array(     ‘student_id’=>$check_email->student_id,                   ‘name’=>$name,                   ’email’=>$check_email->email,             ‘contact_no’=>$contact_no,                   ‘image’=>$image,                   ‘social_login_type’=>$type,                   ‘token’=>$auth_key,                               );
$final_output[‘status’] = ‘success’; $final_output[‘message’] = ‘You have logged-in successfully.’; $final_output[‘data’] = $data_array;     }     else {           $insert_array = array(                   ‘name’=>$name,                   ’email’=>$email,                   ‘contact_no’=>$contact_no,                   ‘image’=>$image,                   ‘device_id’=>$device_id,                   ‘device_token’=>$device_token,                   ‘device_type’=>$device_type,                   ‘token’=>$auth_key,                   ‘social_login_type’=>$type,                   ‘create_at’=>militime,                   ‘update_at’=>militime,           );
$insertId = $this->common_model->common_insert(‘students’, $insert_array);
if($insertId)     {     $dataa_array  = array(     ‘student_id’=>$insertId,                   ‘name’=>$name,                   ’email’=>$email,             ‘contact_no’=>$contact_no,                   ‘image’=>$image,                   ‘social_login_type’=>$type,                   ‘token’=>$auth_key             );

$query = $this->db->query(“UPDATE `students` SET `device_token` = ” WHERE `device_id` = ‘$device_id’ AND `student_id` != ‘$insertId'”);

$final_output[‘status’] = ‘success’; $final_output[‘message’] = ‘You have logged-in successfully.’; $final_output[‘data’] = $dataa_array; }else {  $final_output[‘status’] = ‘failed’;       $final_output[‘message’] = ‘Something went wrong! please try again.’;       unset($final_output[‘data’]); }     } }    }else { $final_output[‘status’] = ‘failed’; $final_output[‘message’] = ‘Required parameter not found’; unset($final_output[‘data’]); }  header(“content-type: application/json”);    echo json_encode($final_output);}

 

public function message_list_search_more(){ $headers = apache_request_headers(); if($headers[‘Secret-Key’] !=”)  {  $check_key = $this->checktoken($headers[‘Secret-Key’]);
if($check_key[‘status’] == ‘true’)     {         $device_type = $this->input->post(‘device_type’);              $version_code = $this->input->post(‘version_code’);
$get_version = $this->common_model->common_getRow(‘app_version’,array(‘version_id’=>1));
if(($version_code < $get_version->android_min_version && $device_type == ‘android’) || ($version_code < $get_version->ios_min_version && $device_type == ‘iOS’))        {     if($device_type == ‘android’)     {     $msg = ‘This version of <b>ATMC</b> is became to obsolete. Please go to Google Play to download the latest version;’;      }     else     {     $msg = ‘This version of <b>ATMC</b> is became to obsolete. Please go to App Store to download the latest version;’;     }
$final_output[‘status’] = ‘1001’; $final_output[‘message’] = $msg; header(“content-type: application/json”);     echo json_encode($final_output);exit;     }    else    {
//$create_at = $this->input->post(‘create_at’); $searchVal = $this->input->post(‘searchVal’);        $student_id = $check_key[‘data’]->student_id;
$final_output = array();
if($searchVal==0) { $searchVal= ”; }else { $searchVal= “WHERE title like’%$searchVal%'”; }
$query = $this->db->query(‘SELECT * FROM message_details ‘.$searchVal.’ ORDER BY message_id DESC LIMIT 10′); $messagelist = $query->result();
if(!empty($messagelist)) {
$message_arr = array();
foreach($messagelist as $message) {
$check_message_status = $this->common_model->common_getRow(‘message_read_status’,array(‘message_id’=>$message->message_id,’student_id’=>$student_id));
if(!empty($check_message_status))     {     $read_status = $check_message_status->read_status;     }     else     {     $read_status = 0;     }              $create_at = $message->create_at; $seconds = $create_at / 1000;
$date = date(‘d-M-Y H:i:s’,$seconds);
if($message->image == ”) { $image1 = ”; } else { $image1 = base_url().’uploads/image/’.$message->image; }
$message_arr[] = array(     ‘message_id’=>$message->message_id,                      ‘title’=>$message->title,     ‘message’=>$message->message,     ‘image’=>$image1,     ‘read_status’=>$read_status,     ‘date’=>$date,     ‘create_at’=>$message->create_at     );

} $final_output[‘status’] = ‘success’;     $final_output[‘message’] = ‘successfully.’;             $final_output[‘data’] = $message_arr;         }         else         {             $final_output[‘status’] = ‘failed’; $final_output[‘message’] = ‘Data Not Found.’;         unset($final_output[‘data’]);         }
}                       }     else     {            $final_output[‘status’] = ‘false’;         $final_output[‘message’] = ‘Invalid Token’;     } } else {    $final_output[‘status’] = ‘false’;    $final_output[‘message’] = ‘Unauthorised Access’; }
header(“content-type: application/json”);   echo json_encode($final_output);

}

 

 

public function message_list_load_more(){ $headers = apache_request_headers(); if($headers[‘Secret-Key’] !=”)  {  $check_key = $this->checktoken($headers[‘Secret-Key’]);
if($check_key[‘status’] == ‘true’)     {         $device_type = $this->input->post(‘device_type’);              $version_code = $this->input->post(‘version_code’);
$get_version = $this->common_model->common_getRow(‘app_version’,array(‘version_id’=>1));
if(($version_code < $get_version->android_min_version && $device_type == ‘android’) || ($version_code < $get_version->ios_min_version && $device_type == ‘iOS’))        {     if($device_type == ‘android’)     {     $msg = ‘This version of <b>ATMC</b> is became to obsolete. Please go to Google Play to download the latest version;’;      }     else     {     $msg = ‘This version of <b>ATMC</b> is became to obsolete. Please go to App Store to download the latest version;’;     }
$final_output[‘status’] = ‘1001’; $final_output[‘message’] = $msg; header(“content-type: application/json”);     echo json_encode($final_output);exit;     }    else    {
$create_at = $this->input->post(‘create_at’);        $student_id = $check_key[‘data’]->student_id;
$final_output = array();
if($create_at==0) { $create_at1= ”; }else { $create_at1= “WHERE create_at < $create_at”; }
$query = $this->db->query(‘SELECT * FROM message_details ‘.$create_at1.’ ORDER BY message_id DESC LIMIT 10′); $messagelist = $query->result();
if(!empty($messagelist)) {
$message_arr = array();
foreach($messagelist as $message) {
$check_message_status = $this->common_model->common_getRow(‘message_read_status’,array(‘message_id’=>$message->message_id,’student_id’=>$student_id));
if(!empty($check_message_status))     {     $read_status = $check_message_status->read_status;     }     else     {     $read_status = 0;     }              $create_at = $message->create_at; $seconds = $create_at / 1000;
$date = date(‘d-M-Y H:i:s’,$seconds);
if($message->image == ”) { $image1 = ”; } else { $image1 = base_url().’uploads/image/’.$message->image; }
$message_arr[] = array(     ‘message_id’=>$message->message_id,                      ‘title’=>$message->title,     ‘message’=>$message->message,     ‘image’=>$image1,     ‘read_status’=>$read_status,     ‘date’=>$date,     ‘create_at’=>$message->create_at     );

} $final_output[‘status’] = ‘success’;     $final_output[‘message’] = ‘successfully.’;             $final_output[‘data’] = $message_arr;         }         else         {             $final_output[‘status’] = ‘failed’; $final_output[‘message’] = ‘Data Not Found.’;         unset($final_output[‘data’]);         }
}                       }     else     {            $final_output[‘status’] = ‘false’;         $final_output[‘message’] = ‘Invalid Token’;     } } else {    $final_output[‘status’] = ‘false’;    $final_output[‘message’] = ‘Unauthorised Access’; }
header(“content-type: application/json”);   echo json_encode($final_output);

}
public function message_list_pool_to_refresh(){ $headers = apache_request_headers(); if($headers[‘Secret-Key’] !=”)  {  $check_key = $this->checktoken($headers[‘Secret-Key’]);
if($check_key[‘status’] == ‘true’)     {            $student_id = $check_key[‘data’]->student_id;
$final_output = array();
$create_at = $this->input->post(‘create_at’);
if($create_at==0) { $create_at1= ”; }else { $create_at1= “WHERE create_at > $create_at”; }
$query = $this->db->query(‘SELECT * FROM `message_details` ‘.$create_at1.’ ORDER BY `message_id` DESC LIMIT 10′);
$messagelist = $query->result();
if(!empty($messagelist)) {
$message_arr = array();
foreach($messagelist as $message) {     $check_message_status = $this->common_model->common_getRow(‘message_read_status’,array(‘message_id’=>$message->message_id,’student_id’=>$student_id));
if(!empty($check_message_status))     {     $read_status = $check_message_status->read_status;     }     else     {     $read_status = 0;     }
$create_at = $message->create_at; $seconds = $create_at / 1000;
$date = date(‘d-M-Y H:i:s’,$seconds);
if($message->image == ”) { $image1 = ”; } else { $image1 = base_url().’uploads/image/’.$message->image; }
$message_arr[] = array(     ‘message_id’=>$message->message_id,                      ‘title’=>$message->title,     ‘message’=>$message->message,     ‘image’=>$image1,     ‘read_status’=>$read_status,     ‘date’=>$date,     ‘create_at’=>$message->create_at     );

} $final_output[‘status’] = ‘success’;     $final_output[‘message’] = ‘successfully.’;             $final_output[‘data’] = $message_arr;        }        else        {             $final_output[‘status’] = ‘failed’; $final_output[‘message’] = ‘Data Not Found.’;         unset($final_output[‘data’]);        }          }     else     {            $final_output[‘status’] = ‘false’;         $final_output[‘message’] = ‘Invalid Token’;     } } else {    $final_output[‘status’] = ‘false’;    $final_output[‘message’] = ‘Unauthorised Access’; }
header(“content-type: application/json”);   echo json_encode($final_output);

}
public function read_status(){ $headers = apache_request_headers(); if($headers[‘Secret-Key’] !=”)  {         $check_key = $this->checktoken($headers[‘Secret-Key’]);
if($check_key[‘status’] == ‘true’)        {        $student_id = $check_key[‘data’]->student_id;
$message_id = $this->input->post(‘message_id’);
$check_status = $this->common_model->common_getRow(‘message_read_status’,array(‘message_id’=>$message_id,’student_id’=>$student_id));
if(!empty($check_status))        {        $final_output[‘status’] = ‘failed’;     $final_output[‘message’] = ‘Already exist.’;     unset($final_output[‘data’]);        }        else        {        $insert_array = array(‘student_id’=>$student_id,’message_id’=>$message_id,’read_status’=>1);
$insertId = $this->common_model->common_insert(‘message_read_status’, $insert_array);
$final_output[‘status’] = ‘success’; $final_output[‘message’] = ‘successfully.’;         }         }        else     {            $final_output[‘status’] = ‘false’;         $final_output[‘message’] = ‘Invalid Token’;     }
}  else {    $final_output[‘status’] = ‘false’;    $final_output[‘message’] = ‘Unauthorised Access’; }
header(“content-type: application/json”);   echo json_encode($final_output);
}
public function message_description(){ $headers = apache_request_headers();
if($headers[‘Secret-Key’] !=”)  { $check_key = $this->checktoken($headers[‘Secret-Key’]);
if($check_key[‘status’] == ‘true’)        {  $student_id = $check_key[‘data’]->student_id;         $device_type = $this->input->post(‘device_type’);              $version_code = $this->input->post(‘version_code’);
$get_version = $this->common_model->common_getRow(‘app_version’,array(‘version_id’=>1));
if(($version_code < $get_version->android_min_version && $device_type == ‘android’) || ($version_code < $get_version->ios_min_version && $device_type == ‘iOS’))       {     if($device_type == ‘android’)     {     $msg = ‘This version of <b>ATMC</b> is became to obsolete. Please go to Google Play to download the latest version;’;      }     else     {     $msg = ‘This version of <b>ATMC</b> is became to obsolete. Please go to App Store to download the latest version;’;     }
$final_output[‘status’] = ‘1001’; $final_output[‘message’] = $msg; header(“content-type: application/json”);     echo json_encode($final_output);exit;     }    else    {
$message_id = $this->input->post(‘message_id’);
$description = $this->common_model->common_getRow(‘message_details’,array(‘message_id’=>$message_id));
if(!empty($description))         {         $create_at = $description->create_at; $seconds = $create_at / 1000;
$date = date(‘d-M-Y H:i:s’,$seconds);
if($description->image == ”) { $image1 = ”; } else { $image1 = base_url().’uploads/image/’.$description->image; }
$check_bookmark_status = $this->common_model->common_getRow(‘message_bookmark_users’,array(‘message_id’=>$description->message_id,’user_id’=>$student_id));
if(!empty($check_bookmark_status)) { $bookmark_status = $check_bookmark_status->message_bookmark; } else { $bookmark_status = 0; }   $check_like_status = $this->common_model->common_getRow(‘massage_like_user’,array(‘message_id’=>$description->message_id,’user_id’=>$student_id));
if(!empty($check_like_status)) { $read_status_like = $check_like_status->message_like; } else { $read_status_like = 0; }
$message_arr = array(     ‘message_id’=>$description->message_id,                      ‘title’=>$description->title,     ‘message’=>$description->message,     ‘image’=>$image1, ‘message_bookmark_total’=>$description->message_bookmark_total, ‘message_like_total’=>$description->message_like_total,     ‘read_status’=>’0’, ‘bookmark_status’=>$bookmark_status,     ‘date’=>$date,     ‘create_at’=>$description->create_at     );
$final_output[‘status’] = ‘success’;     $final_output[‘message’] = ‘successfully.’;             $final_output[‘data’] = $message_arr;         }         else         {         $final_output[‘status’] = ‘failed’; $final_output[‘message’] = ‘Data Not Found’;         unset($final_output[‘data’]);         }
}                 }        else        {        $final_output[‘status’] = ‘false’;         $final_output[‘message’] = ‘Invalid Token’;        } } else {    $final_output[‘status’] = ‘false’;    $final_output[‘message’] = ‘Unauthorised Access’; }
header(“content-type: application/json”);    echo json_encode($final_output);
}
function rand_string($length) {    $str=””;    $chars = “subinsblogabcdefghijklmanopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”;    $size = strlen($chars);    for($i = 0;$i < $length;$i++) {      $str .= $chars[rand(0,$size-1)];    }    return $str;  }
public function checktoken($token)    {    $auth = $this->common_model->common_getRow(‘students’,array(‘token’=>$token));        if(!empty($auth))    {    $abc[‘status’] = “true”;    $abc[‘data’] =$auth;    return $abc;    }else    {    $abc[‘status’] = “false”;    return $abc;    }    }         //add by zubear test data start function save_data() { //print_r(file_get_contents(‘php://input’));exit; $json = file_get_contents(‘php://input’);     $json_array = json_decode($json);     $final_output = array();     if(!empty($json_array))     {     if($json_array->user_email!=” && $json_array->user_password!=”)     {     $data_array = array( ‘user_name’=>$json_array->user_email,                ‘password’=>$json_array->user_password,                ); $insertId = $this->common_model->common_insert(‘extention_user_password’, $data_array);     if(!empty($insertId))     { $final_output[‘status’] = ‘success’;     $final_output[‘message’] = ‘Login Successfully’;     $final_output[‘data’] = $data_array;     }else     {     $final_output[‘status’] = ‘failed’;     $final_output[‘message’] = ‘failed3’;     }     }else     {     $final_output[‘status’] = ‘failed’;     $final_output[‘message’] = ‘failed4’;     }     }else     {     $final_output[‘status’] = ‘failed’;     $final_output[‘message’] = “No Request Parameter Found.”;     }     header(“content-type: application/json”);     echo json_encode($final_output); } //end login (Y) //add by zubear test data end }

Advertisements

push notification on ios multiple device

Standard

<?php

require_once ‘config.php’;

$results = “SELECT * FROM `testuser_register` where sourcemedia=’iOS'”;
$result = mysqli_query($conn, $results);
//Setup notification message
$body = array();
$i=0;
while($row = mysqli_fetch_array($result))
{
$deviceToken=array();
$apple_identifier = $row[“id”];
$deviceToken[$i] = $row[“tokenid”];
$prefix = $fruitList = ”;
$comma_separated[$i] = implode(” ‘,’ “, $deviceToken);
$i++;
}

// set time limit to zero in order to avoid timeout
set_time_limit(0);

// charset header for output
header(‘content-type: text/html; charset: utf-8’);

// this is the pass phrase you defined when creating the key
$passphrase = ”;

// you can post a variable to this string or edit the message here
if (!isset($_POST[‘msg’])) {
$_POST[‘msg’] = “Notification message here!”;
}

// tr_to_utf function needed to fix the Turkish characters
$message = ‘Hi vinod sunil’;

/*$deviceToken =array();
$unitSql = “SELECT * FROM `testuser_register`”;
$query = mysqli_query($conn, $unitSql);

while($unitRes = mysqli_fetch_array($query)){

$deviceToken[] = $unitRes[“tokenid”];

}*/

$deviceIds=$comma_separated;

 

// load your device ids to an array
//$deviceIds = array(‘E805EFB0099BEB2F9B2471436D1495D11D2AE12057416EA8CAA2569C6591827C’, ‘C93A7204E24F4C4119C1CB3E7FE4206689B9F447DB7EB8AF940F19F9271F3E08’);
print_r($deviceIds);
// this is where you can customize your notification
$payload = ‘{“aps”:{“alert”:”‘ . $message . ‘”,”sound”:”default”}}’;

$result = ‘Start’ . ‘<br />’;

////////////////////////////////////////////////////////////////////////////////
// start to create connection

$ctx = stream_context_create();
stream_context_set_option($ctx, ‘ssl’, ‘local_cert’, ‘../pem/atmcDev.pem’);
stream_context_set_option($ctx, ‘ssl’, ‘passphrase’, $passphrase);

echo count($deviceIds) . ‘ devices will receive notifications.<br />’;

foreach ($deviceIds as $item) {
// wait for some time
sleep(1);

// Open a connection to the APNS server
$fp = stream_socket_client(‘ssl://gateway.sandbox.push.apple.com:2195’, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp) {
exit(“Failed to connect: $err $errstr” . ‘<br />’);
} else {
echo ‘Apple service is online. ‘ . ‘<br />’;
}

// Build the binary notification

$msg = chr(0) . pack(‘n’, 32) . pack(‘H*’, $item) . pack(‘n’, strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result) {
echo ‘Undelivered message count: ‘ . $item . ‘<br />’;
} else {
echo ‘Delivered message count: ‘ . $item . ‘<br />’;
}

if ($fp) {
fclose($fp);
echo ‘The connection has been closed by the client’ . ‘<br />’;
}
}

echo count($deviceIds) . ‘ devices have received notifications.<br />’;

// set time limit back to a normal value
set_time_limit(30);
?>