IMMREX7
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Http;
use Carbon\Carbon;
use Exception;
use Google\Client;
use Google\Service\Docs;
/**
* Description of GenerateReport
*
* @author Lenovo
*/
class GpsReport {
static function sendNotification($json) {
return self::sendMessage($json);
}
static function getGoogleAccessToken(){
$credentialsFilePath = storage_path().'/app/gps-events-3ab65-79ea8c6fde4d.json';
$client = new \Google_Client();
$client->setAuthConfig($credentialsFilePath);
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$client->refreshTokenWithAssertion();
$token = $client->getAccessToken();
return $token['access_token'];
}
static function sendMessage($json){
$apiurl = 'https://fcm.googleapis.com/v1/projects/gps-events-3ab65/messages:send';
$headers = [
'Authorization: Bearer ' . self::getGoogleAccessToken(),
'Content-Type: application/json'
];
$notifications = [
'title' => $json["title"],
'body' => $json["body"],
];
$data = [
"title" => $json["title"],
"body" => $json["body"]
];
$message = [
'message' => [
'token' => $json["token"],
'notification' => $notifications,
'data' => $data,
],
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message));
$result = curl_exec($ch);
if ($result === FALSE) {
//Failed
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
//put your code here
static function generateReport() {
ini_set('memory_limit', '-1');
date_default_timezone_set("Asia/Kolkata");
try{
$url = 'https://mvts7.millitrack.com/api/middleMan/getDeviceInfo?accessToken=ZXlKaGJHY2lPaUpJVXpJMU5pSjkuZXlKemRXSWlPaUkwTkRjeE55SXNJbWx6Y3lJNkltZHdjeTEwY21GamEyVnlJaXdpYVdGMElqb3hOekkwTXpRMU56RTFmUS5FSjItaVBTZ00wY2Z5SE5OZGhnUFJ0bWhsUk5xRTQ0VTlGd0xKa0ZndUJN';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => false,
));
//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//get response
$output = curl_exec($ch);
//return $output;
$json_response = json_decode($output);
if ($json_response != null) {
foreach ($json_response->object as $var) {
$challan = \App\DeviceReport::where('vehicle_no', '=', $var->name)->first();
if (empty($challan)) {
$reports = new \App\DeviceReport();
$reports->vehicle_no = $var->name;
$now = Carbon::now();
$gps = Carbon::parse($var->lastStatusUpdate);
$diff = $gps->diffInDays($now, false);
if($diff >= 1) $reports->status = 0;
else $reports->status = 1;
$reports->corn_time = $now->format('Y-m-d');
$reports->gps_update = $gps->format('Y-m-d H:i:s');
$reports->save();
}else{
$now = Carbon::now();
$gps = Carbon::parse($var->lastStatusUpdate);
$diff = $gps->diffInDays($now, false);
if($diff >= 1) $challan->status = 0;
else $challan->status = 1;
$challan->corn_time = $now->format('Y-m-d');
$challan->gps_update = $gps->format('Y-m-d H:i:s');
$challan->update();
}
}
}
curl_close($ch);
return $output;
}catch(Exception $e){
return $e->getMessage();
}
}
}
Copyright © 2021 -