Belgheys
Belgheys

Reputation: 1

in Laravel How to post data to another route in a route ?

I have a Post Route in Laravel, so if a user post data to the route I want to add data to request then posting it to another post route that I have then show the user the result that I get from the second route.
Is there way to do it? or I should use GuzzleHttp?

Upvotes: 0

Views: 868

Answers (1)

Elvis Mugabi
Elvis Mugabi

Reputation: 347

You can use curl to perform the sub request.

Route::any('/proxy', function (Request $request) {

    //Get all of the input from the request and store them in $data. 
    $data = $request->all();

    //initialise your other data. 
    $data2 = ['xyz' => "xyz"];

    $newPayload = array_merge($data, $data2);

    $endPoint = "https://example.org";

    $ch = curl_init($endPoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $newPayload);

    // execute!
    $response = curl_exec($ch);

    // close the connection, release resources used
    curl_close($ch);

    return (string)$response;
});

Upvotes: 0

Related Questions