JezK
Edit File: tripcron.php
<?php //#!/usr/bin/php //@ob_end_clean(); date_default_timezone_set("Asia/Calcutta"); include("Config.php"); $con = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD); $con = mysql_select_db(DB_NAME) or die(mysql_error()); $today_date = date('Y-m-d'); define("DRVIMGSIZE","640x640"); define("MAPTYPE","roadmap"); define("FTYPE","png"); $check_time = '10'; $tripids=array(); //$today_date = '2016-03-28'; $qry = "select trip_id,Created_on from ced_t_trip_history where Created_on = '".$today_date."'"; $exe=mysql_query($qry); if(mysql_num_rows($exe)) { while($row=mysql_fetch_array($exe)) { //$trip_ids[]=$row; $tripids[] = $row['trip_id']; } } if(count($tripids)) { foreach($tripids as $key=>$val) { $qry_geo = "select * from `ced_t_geovalues` WHERE `trip_id` = '".$val."' order by geo_id desc limit 0,1"; $exe_geo = mysql_query($qry_geo); $row_geo=mysql_fetch_array($exe_geo); if($row_geo['journey_status'] == "2") { $last_time = strtotime($row_geo['taken_at'])."<br>"; $nowtime = strtotime(date("Y-m-d H:i:s"))."<br>"; $interval = abs($nowtime - $last_time); $minutes = round($interval / 60); if($minutes > $check_time) { $qry="update ced_t_geovalues set journey_status = '3' where geo_id = '".$row_geo['geo_id']."' "; $exe=mysql_query($qry); if($exe) { $tripcord = makethisimage($row_geo['trip_id']); $url='https://maps.googleapis.com/maps/api/staticmap?size='.DRVIMGSIZE.'&maptype='.MAPTYPE.'&path=color:0xff0000ff|weight:5'.$tripcord[travel].'markers=color:blue%7Clabel:S%7C'.$tripcord[source].'&markers=color:green%7Clabel:D%7C'.$tripcord[destination].'&format='.FTYPE; $filename=$trip_id.date('Ymdhis').'.png'; $dir = '../mobapp/uploads/'.$filename; file_put_contents($dir ,file_get_contents($url)); $endtime=date('h:i a'); $trip_id = $row_geo['trip_id']; $qry="update ced_t_trip_history set map_img='".$filename."',end_time='".$endtime."' where trip_id=".(int)$trip_id; $exe = mysql_query($qry); $sql2="SELECT p.`device_id`,p.`school_id`,r.`academic_year_id`,r.`student_id` FROM `ced_t_students_parent` p inner join `ced_t_students_routes` r on r.`student_id`= p.`student_id` inner join `ced_t_students` s on s.`student_id`=p.`student_id` inner join `ced_t_student_licenses` li on li.`student_id`= s.`student_id` inner join `ced_t_authentication_details` au on au.`authenticate_id`=p.`authenticate_id` where r.`route_id`='".$row_geo['route_id']."' and s.`status`='1' and p.`account_status`='1' and li.`license_status`='1' and au.`notifications`='1'"; $result2=mysql_query($sql2); if(mysql_num_rows($result2)>0) { while($row=mysql_fetch_array($result2)) { $message['message'] = "School Bus has completed the trip"; $message['type'] = 'trip_comp'; $message['result'] = "no"; $message['date'] = date('Y-m-d'); $message['trip_id'] = $row_geo['trip_id']; $message['student_id'] = $row['student_id']; $registatoin_ids = array($row['device_id']); if(($row['device_id']!='')) { $value=send_notification($registatoin_ids,$message); } } } } } } } } function makethisimage($tripid) { $strlatlng=''; $i=1; $sql="select latitude,longitude from ced_t_geovalues where trip_id=".(int)$tripid." order by geo_id asc"; $exe=mysql_query($sql); $upto=mysql_num_rows($exe); if(mysql_num_rows($exe)) { while($row=mysql_fetch_array($exe)) { if($i==1) { $start=$row[latitude].','.$row[longitude]; } $strlatlng.='|'.$row[latitude].','.$row[longitude]; if($i==$upto) { $end=$row[latitude].','.$row[longitude]; } $i++; } $data['source']=$start; $data['destination']=$end; $data['travel']=$strlatlng; return $data; } } function send_notification($registatoin_ids, $message) { //echo json_encode($registatoin_ids) .':'.json_encode($message).':'.$googleapi; $url = 'https://android.googleapis.com/gcm/send'; $googleapi='AIzaSyDZjnc0dm76JpZpk5mJSw_oOPgfoDSSbR0'; $fields = array( 'registration_ids' => $registatoin_ids, 'data' => $message ); $headers = array( 'Authorization: key='.$googleapi, 'Content-Type: application/json' ); // Open connection $ch = curl_init(); // Set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Disabling SSL Certificate support temporarly curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); // Execute post $result = curl_exec($ch); if ($result === FALSE) { die('Curl failed: ' . curl_error($ch)); } curl_close($ch); //echo $result; return $result; } ?>