Skip to content

RPC支持header参数 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ _book
*.epub
*.mobi
*.pdf
*.idea
*.idea

vendor
composer.lock
17 changes: 13 additions & 4 deletions src/RPC/RpcProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Request;
use PhpBoot\Application;
use PhpBoot\Controller\ControllerContainer;
use PhpBoot\Controller\ControllerContainerBuilder;
Expand Down Expand Up @@ -50,12 +51,17 @@ public function __call($method, $args)

$request = $this->createRequest($method, $route, $args);

$options = [
'http_errors' => false,
'headers' => $request->getHeaders(),
'body' => $request->getBody()
];
if(MultiRpc::isRunning()){
$op = $this->http->sendAsync($request,['http_errors' => false]);
$op = $this->http->requestAsync($request->getMethod(), $request->getUri(), $options);
$res = MultiRpc::wait($op);
return $this->mapResponse($method, $route, $res, $args);
}else{
$res = $this->http->send($request,['http_errors' => false]);
$res = $this->http->request($request->getMethod(), $request->getUri(), $options);
return $this->mapResponse($method, $route, $res, $args);
}
}
Expand All @@ -64,7 +70,7 @@ public function __call($method, $args)
* @param $actionName
* @param Route $route
* @param array $args
* @return RequestInterface
* @return Request
*/
public function createRequest($actionName, Route $route, array $args)
{
Expand Down Expand Up @@ -105,7 +111,10 @@ public function createRequest($actionName, Route $route, array $args)
unset($request['query']);

if(isset($request['headers'])){
$headers += $request['headers'];
// 下划线转中划线,保持接口参数绑定一致
foreach ($request['headers'] as $headerKey => $headerValue) {
$headers += [str_replace('_', '-', $headerKey) => $headerValue];
}
}
unset($request['headers']);

Expand Down