簡體   English   中英

Symfony FosRest使用端點API

[英]Symfony FosRest use end point api

我需要在操作中調用某種方法(GET,POST或PUT),該怎么做。 示例我有PUT端點

    /**
 * 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實體Lead,在此職位Lead中,我需要調用“ 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方法,會導致url的獲取和放置相等,並且可能默認情況下用於重定向GET方法

但是我不需要返回,我需要調用一些潰敗,並且仍然可以通過使用“ out_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>

我用這個,但是如何在Guzzle的幫助下使用呢?

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;
}

您可以使用PHP cURL進行HTTP請求或安裝PHP HTTP Client(例如Guzzle)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM