[英]PHP - Calling internal API
我是PHP的新手。 我使用* composer **創建了一個Laravel項目。 我的控制器有兩個端點uploadFile和testpost :
public function uploadFile(Request $request) {
//there are more code about reading uploaded file here. Everything is OK here.
$request = Request::create('/api/testpost', 'POST',
[],[],[],[],'{"this is" : "my test content"}');
return Route::dispatch($request);
}
public function testpost(Request $request){
Log::info($request->all());
return response()->json(["title"=>"this is the test get method"]);
}
POST操作從帶有上載JSON文件的表單中調用uploadFile 。 我想使用Request :: create(...)和Route :: dispatch(...)在uploadFile方法內部調用testpost 。 testpost被調用,但是請求的主體不符合預期。 日志文件顯示$ request-> all()不會返回我希望是{“ this is”:“ my test content”}的請求正文。
我的日志文件:
[2019-02-23 12:16:47] local.INFO: array (
'_token' => 'JzQjclRD4WaTkezqLxlU48D1dM7S3X2X3hok3kr4',
'employee_file' =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => 'test_input_file.txt',
'mimeType' => 'text/plain',
'error' => 0,
'hashName' => NULL,
)),
)
我的代碼有什么問題? API調用還是請求主體檢索? 我知道我們可以直接調用testpost方法而不是調用API。 但是,我的最終目的是要知道如何調用內部API。
您無需為此使用內部API調用。
如果兩個方法都在同一個類中,則可以直接使用
$this->methodName($args);
它將結果直接返回到調用函數。 (只要您在調用的方法中具有return語句)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.