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

har*_*B10 5 php laravel

我有两条路线.

Route::get('/receiveSignal', 'SignalController@receiveSignal');
Route::get('/sendSignal', 'SignalController@sendSignal');
Run Code Online (Sandbox Code Playgroud)

我想模拟从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;

    }
Run Code Online (Sandbox Code Playgroud)

如何更改它以便receiveSignal像这样接收:

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

        dd($test);
    }
Run Code Online (Sandbox Code Playgroud)

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

Olu*_*deh 0

尝试这样的操作: 1. 在您的路线中:

Route::post('receiveSignal', 'SignalController@receiveSignal');
Route::get('sendSignal', 'SignalController@sendSignal');
Run Code Online (Sandbox Code Playgroud)
  1. 在您的 sendSignal 方法中

    公共函数sendSignal()
    {
        $data = ['key' => 'value', 'key2' => 'value2'];
        $response = http_post_fields('http://localhost:8000/receiveSignal', $data);
        if (!empty($response)) {
            返回视图('成功'); // 或任何你想返回的东西
        }
        别的 {
            返回视图('失败');
        }
     }
    
  2. 在您的 receiveSignal 方法中

    公共函数 receiveSignal(请求 $request)
    {
        $key = $request->input('key');
        $key1 = $request->input('key2');
        //等等
    }
    

祝你好运。