【问题标题】:Simulate sending data and receiving using php://input使用 php://input 模拟发送和接收数据
【发布时间】:2018-06-26 09:01:04
【问题描述】:

我有两条路线。

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。这可能吗?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

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

    Route::post('receiveSignal', 'SignalController@receiveSignal');
    Route::get('sendSignal', 'SignalController@sendSignal');
    
    1. 在你的 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'); //等等 }

    祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 2011-08-06
      • 2020-02-09
      • 2019-09-12
      • 1970-01-01
      • 2016-04-03
      • 2011-05-18
      相关资源
      最近更新 更多