Simulate sending and receiving data using php: // input

I have two routes.

Route::get('/receiveSignal', ' SignalController@receiveSignal '); Route::get('/sendSignal', ' SignalController@sendSignal '); 

I want to simulate sending data from sendSignal to the receive signal path.

So, in the signal sending function, I have the following:

 public function sendSignal() { $data = ['spotid' => '421156', 'name' => 'Test', 'desc' => 'some desc', 'StartofDetection' => '2018-01-17 22:22:22']; $dataJson = json_encode($data); return $dataJson; } 

How can I change it to receive in receiveSignal as follows:

 public function receiveSignal() { $test = file_get_contents('php://input'); dd($test); } 

Here I have to get json for receiveSignal after entering http: // localhost: 8000 / sendSignal . Is this even possible?

+5
source share
1 answer

Try something like this: 1. On your route:

 Route::post('receiveSignal', ' SignalController@receiveSignal '); Route::get('sendSignal', ' SignalController@sendSignal '); 
  1. In your sendSignal method

      public function sendSignal ()
     {
         $ data = ['key' => 'value', 'key2' => 'value2'];
         $ response = http_post_fields ('http: // localhost: 8000 / receiveSignal', $ data);
         if (! empty ($ response)) {
             return view ('success');  // or anything else you want to return
         }
         else {
             return view ('failed'); 
         }
      }
    
  2. In your getSignal method

      public function receiveSignal (Request $ request)
     {
         $ key = $ request-> input ('key');
         $ key1 = $ request-> input ('key2');
         // and so on
     }
    

Good luck.

0
source

Source: https://habr.com/ru/post/1274757/


All Articles