我有两条路线。
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. 在你的 route :
Route::post('receiveSignal', 'SignalController@receiveSignal');
Route::get('sendSignal', 'SignalController@sendSignal');
在您的 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'); } }
在您的 receiveSignal 方法中
public function receiveSignal(Request $request) { $key = $request->input('key'); $key1 = $request->input('key2'); //and so on }
祝你好运。
关于php - 使用php模拟发送和接收数据://input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48301815/