使用php:// input模拟发送数据和接收

时间:2018-01-17 12:59:55

标签: php laravel

我有两条路线。

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

我想模拟从sendSignal向接收信号路径发送数据。

所以,在发送信号功能时,我有这个:

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

        $dataJson = json_encode($data);

        return $dataJson;

    }

如何将其更改为receiveSignal,如下所示:

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

        dd($test);
    }

在我输入http://localhost:8000/sendSignal后,我应该在receiveSignal收到json。这有可能吗?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情: 1.在你的路线:

Route::post('receiveSignal', 'SignalController@receiveSignal');
Route::get('sendSignal', 'SignalController@sendSignal');
  1. 在您的sendSignal方法

    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. 在您的receiveSignal方法

    public function receiveSignal(Request $request)
    {
        $key = $request->input('key');
        $key1 = $request->input('key2');
        //and so on
    }
    
  3. 祝你好运。