From e72f0bf55d833f4125b05955a67e94ef0bb453cb Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Wed, 15 Nov 2017 06:49:29 -0800 Subject: [PATCH] send the actual location when pinging an external URL --- compass/app/Http/Controllers/Api.php | 6 ++++- compass/app/Jobs/NotifyOfNewLocations.php | 27 ++++++++++++++--------- compass/app/Jobs/TripComplete.php | 4 +++- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/compass/app/Http/Controllers/Api.php b/compass/app/Http/Controllers/Api.php index 028d31d..d6bf447 100644 --- a/compass/app/Http/Controllers/Api.php +++ b/compass/app/Http/Controllers/Api.php @@ -269,6 +269,10 @@ class Api extends BaseController } } + if($request->input('current')) { + $last_location = $request->input('current'); + } + $response = [ 'result' => 'ok', 'saved' => $num, @@ -289,7 +293,7 @@ class Api extends BaseController // Notify subscribers that new data is available if($db->ping_urls) { - $job = (new NotifyOfNewLocations($db->id))->onQueue('compass'); + $job = (new NotifyOfNewLocations($db->id, $last_location))->onQueue('compass'); $this->dispatch($job); } } diff --git a/compass/app/Jobs/NotifyOfNewLocations.php b/compass/app/Jobs/NotifyOfNewLocations.php index 667c1b6..74b2d10 100644 --- a/compass/app/Jobs/NotifyOfNewLocations.php +++ b/compass/app/Jobs/NotifyOfNewLocations.php @@ -1,8 +1,7 @@ _dbid = $dbid; + $this->_last_location = $last_location; } public function handle() { $db = DB::table('databases')->where('id','=',$this->_dbid)->first(); $urls = preg_split('/\s+/', $db->ping_urls); foreach($urls as $url) { - $ch = curl_init($url); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ - 'url' => env('BASE_URL').'api/last?token='.$db->read_token.'&geocode=1' - ])); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_exec($ch); + if(trim($url)) { + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + 'Authorization: Bearer '.$db->read_token, + 'Compass-Url: '.env('BASE_URL').'api/last?token='.$db->read_token.'&geocode=1' + ]); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->_last_location)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_exec($ch); + Log::info("Notifying ".$url." with current location"); + } } } } diff --git a/compass/app/Jobs/TripComplete.php b/compass/app/Jobs/TripComplete.php index 7600318..1e8eea2 100644 --- a/compass/app/Jobs/TripComplete.php +++ b/compass/app/Jobs/TripComplete.php @@ -238,7 +238,9 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue $response = curl_exec($ch); Log::info("Done!"); - Log::info($response); + if(preg_match('/Location: (.+)/', $response, $match)) { + Log::info($match[1]); + } // echo "========\n"; // echo $response."\n========\n";