1
我需要在我的動作中調用一些方法(GET,POST或PUT),這是怎麼回事。示例I具有PUT端點Symfony FosRest使用終點api
/**
* Update existing Team from the submitted data or create a new Team.
*
* @ApiDoc(
* resource = true,
* description = "Create/Update single Team",
* parameters={
* {"name"="name", "dataType"="string", "required"=false, "description"="image_back"}
* },
* statusCodes = {
* 200 = "Team successful update",
* 400 = "Secret token is not valid"
* },
* section="Team"
*)
* @RestView()
*
* @param Request $request
* @param int $id
*
* @return View
*/
public function putTeamAction(Request $request, $id)
並且此端點具有路由「put_team」。如何調用這個動作在另一個控制器,比如我叫POST實體鉛和在這個崗位鉛我需要調用「put_team」:
/**
* Post Lead.
*
* @ApiDoc(
* resource = true,
* description = "Post lead",
* parameters={
* {"name"="title", "dataType"="string", "required"=true, "description"="title lead"},
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned secret token is not valid"
* },
* section="Lead"
*)
*
* @RestView()
*
* @param Request $request
*
* @return View
*
* @throws NotFoundHttpException when not exist
*/
public function postLeadAction(Request $request)
{
如果我試試這個
return $this->redirect($this->generateUrl('put_team', array('id' => 31)));
但調用get方法, bacause網址獲取和放置平等,也許是因爲在默認情況下GET方法重定向
但我不需要回報,我需要調用一些潰敗並在第一個動作仍能正常工作,使用的答案被潰敗「put_eam」 我嘗試
$test = $this->redirect($this->generateUrl('put_team', array('id' => 31)));
,但有狀態碼302和
內容的HTML有:
<body>
Redirecting to <a href="/app_dev.php/api/teams/31">/app_dev.php/api/teams/31</a>.
</body>
我用這一點,但如何像這樣的幫助狂飲使用?
function send_messages_aog($senders_token, $id, $title){
$url = BASE_URL.'/api/teams/'.$id;
$data = array('senders_token' => $senders_token, 'title' => $title);
$options = array(
'http' => array(
'method' => 'PUT',
'content' => json_encode($data),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
return $response;
}
解決
我用狂飲
public function updateTalent($userId, $token)
{
$guzzle = new Client();
$putTeam = $guzzle
->put($this->router->generate('put_developer', ['id' => $userId], UrlGeneratorInterface::ABSOLUTE_URL),
[],
json_encode([
"token" => $token,
])
)
->send()
->getBody(true);
$answer = json_decode($putTeam, true);
return $answer;
}
我更新我的問題,如何使用Guuzzle就像我使用file_get_contents? –