PHP-调用内部API

PHP-调用内部API,php,laravel,Php,Laravel,我是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',

我是PHP的新手。 我使用*composer**创建了一个Laravel项目。 我的控制器有两个端点uploadFiletestpost

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"]);
}
uploadFile由POST操作从携带上载JSON文件的表单调用。 我想使用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语句)

我知道你的意思。实际上,我需要创建两个项目,一个用于API,另一个用于调用该API的web应用程序。我只是做了第一步,通过像那样调用内部API来学习如何使用PHP调用API。这是一种非常奇怪的发送请求的方法,我认为您出现问题的原因是原始请求与您手动创建的请求重叠。我希望在.Net中使用类似于HttpClient的东西,但我找不到它。如果您使用的是
laravel/installer
,那么您也在全局安装了
GuzzleHttp/Guzzle
包。这就是你想用的。文件: