简体   繁体   English

Symfony FosRest使用端点API

[英]Symfony FosRest use end point api

I need call some method (GET, POST or PUT) in my action, how do this. 我需要在操作中调用某种方法(GET,POST或PUT),该怎么做。 Example I have PUT endpoint 示例我有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)

and this endpoint have rout "put_team". 并且该端点具有溃败“ put_team”。 How to call this action in another controller, example I call POST entity Lead and in this post lead I need call "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)
{

If I try this 如果我尝试这个

    return $this->redirect($this->generateUrl('put_team', array('id' => 31)));

but call get methods, bacause url get and put equal and maybe for redirect by default GET method 但是调用get方法,会导致url的获取和放置相等,并且可能默认情况下用于重定向GET方法

But I don't need return, I need call some rout and still work in first action, using answer by rout "put_eam" I try 但是我不需要返回,我需要调用一些溃败,并且仍然可以通过使用“ out_eam”溃败的答案来进行第一个动作

$test = $this->redirect($this->generateUrl('put_team', array('id' => 31)));

but have status code 302 and 但状态码为302,

in content html with: 在内容html中具有:

<body>
    Redirecting to <a href="/app_dev.php/api/teams/31">/app_dev.php/api/teams/31</a>.
</body>

I use this but how use like this with help Guzzle ? 我用这个,但是如何在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;
}

SOLVED 解决了

I use Guzzle 我用牛油

    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