Advertisement
aitron

Untitled

Feb 15th, 2020
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1.     public function handle()
  2.     {
  3.         // $res=array();
  4.         // $data=DB::table('devices')
  5.         // ->select('name','uniqueId','lastValidLatitude as latitude','lastValidLongitude as longitude','speed','address','device_time','server_time')
  6.         // ->where('vendor',2)
  7.         // ->whereNotNull('latestPosition_id')
  8.         // ->get();
  9.         // $i=0;
  10.         // foreach ($data as $e) {
  11.         //     $res[$i]['DeviceId']=$e->uniqueId;
  12.         //     $res[$i]['Name']=$e->name;
  13.         //     $res[$i]['lat']=$e->latitude;
  14.         //     $res[$i]['lng']=$e->longitude;
  15.         //     $res[$i]['TimeServer']= date('Y-m-d H:i:s');
  16.         //     $res[$i]['Speed']= $e->speed;
  17.         //     $res[$i]['GpsDateTime']= $e->device_time;
  18.         //     $res[$i]['GpsDateTimeUtc']= $e->server_time;
  19.         //     $res[$i]['TransferDateTimeUtc']= date('Y-m-d H:i:s');
  20.         //     $url = 'https://gps.aitron.id/api/geo_address?lat='.$res[$i]['lat'].'&lon='.$res[$i]['lng'];
  21.         //     $ch = curl_init();
  22.         //     curl_setopt($ch, CURLOPT_URL, $url);
  23.         //     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  24.         //     $resp = curl_exec($ch);
  25.         //     curl_close($ch);
  26.         //     $res[$i]['alamat_sekarang'] = $resp;
  27.         //     $i++;
  28.         // }
  29.  
  30.         $dataToUpload=array();
  31.         $data=DB::table('devices')
  32.         ->select('name','uniqueId','lastValidLatitude as latitude','lastValidLongitude as longitude','speed','address','device_time','server_time')
  33.         ->where('vendor',2)
  34.         ->whereNotNull('latestPosition_id')
  35.         ->get();
  36.         $i=0;
  37.    
  38.         $parentData = [
  39.            "GPSProvider"=>"Kabut",
  40.            "Username"=>"Kabut",
  41.            "Password"=>"P@ssw0rd",
  42.            "DataToUpload" => []
  43.         ];
  44.        
  45.         foreach ($data as $e) {
  46.              $dataToUpload[] = [
  47.                'DeviceId' => $e->uniqueId,
  48.                'Name' => $e->name,
  49.                'lat' => $e->latitude,
  50.                'lng' => $e->longitude,
  51.                'TimeServer' => date('Y-m-d H:i:s'),
  52.                'Speed' => $e->speed,
  53.                'GpsDateTime' => $e->device_time,
  54.                'GpsDateTimeUtc' => $e->server_time,
  55.                'TransferDateTimeUtc' => date('Y-m-d H:i:s')
  56.              ];
  57.         }
  58.    
  59.         //ini data masih berupa array php
  60.         $parentData["DataToUpload"] = $dataToUpload;
  61.    
  62.         //jadikan fromat json, data ini yg akan di lemparkan ke api pakai curl
  63.         $data = json_encode($parentData);
  64.        
  65.         for ($i=0; $i<count($dataToUpload); $i++) {
  66.             $ch = curl_init( "http://linfox.southeastasia.cloudapp.azure.com/LinfoxINDO/RESTServices/GPSDataService.svc/Upload" );
  67.  
  68.             $parentData = array(
  69.                 "VehicleNumber" => $dataToUpload[$i]['Name'],
  70.                 "Latitude" => $dataToUpload[$i]['lat'],
  71.                 "Longitude" => $dataToUpload[$i]['lng'],
  72.                 "TransferDateTime" => $dataToUpload[$i]['TimeServer'],
  73.                 "Location" => 'Testing',
  74.             );
  75.             $payload = json_encode($dataToUpload);
  76.             curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
  77.             curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
  78.             curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  79.             $result = curl_exec($ch);
  80.             curl_close($ch);
  81.             echo "<pre>";
  82.             print_r($result);
  83.             // \Log::info($result);
  84.         }
  85.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement