1

I need call some method (GET, POST or PUT) in my action, how do this. Example I have PUT endpoint

    /**
 * 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". 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.
 *
 * @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

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

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

but have status code 302 and

in content html with:

<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 ?

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

1 Answer 1

1

you can do HTTP requests with PHP cURL or install a PHP HTTP Client like Guzzle.

Sign up to request clarification or add additional context in comments.

1 Comment

I update my question, how using Guuzzle like I using file_get_contents ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.