Cerbix's avatar

Passing Form Submitted HTTP POST variables into controller.

So I'm building an application on Laravel that allows my customers to submit a form submitted HTTP POST request from their site to a page on my site, it will then take the POST header data they sent, and use the info they sent to pull data from an external API (Not on my site.)

Sounds basic, problem is I come from basic PHP, and I've been told to avoid using as much PHP logic inside your blade.php views as possible. And instead use the controllers to complete these types of tasks.

Here's an example of what I'm doing:

So my Customers would do a basic Form HTTP POST request (From their site.):


<form action="http://mysite/test" method="POST">

    <input type='hidden' name='LINK_ID' value='test123'>
    <input type='hidden' name='TOKEN_ID' value='token123'>

    <input type="submit">

</form>

Problem is I can't figure out how to pass these variables POST'ed by the customers site, into the controller.

In basic PHP I would typically do a basic get_request and convert it with php into the link as needed:


<?php

//Import from POST data sent.
$imported_id = $_GET['LINK_ID'];

//Add imported Data to Link
$api_link_with_id = "http:MyTestSite/api/" . $imported_id; 

//This would output: http:MyTestSite/api/test123


//Get Files from the adjusted link above
$api = file_get_contents($api_link_with_id);

?>

I would then take the API data and use it as needed to display the information the customers seek.

Now how would I do this same task inside a controller? I did some research and see most people suggest using Guzzle to import the API's json data. (as a substitute to file_get_contents)

Here's an example of the Controller I've been trying to play with:

I need to pass the imported LINK_ID and TOKEN_ID variable into the public function called getRemoteData:


<?php

namespace App\Http\Controllers;
use App\Http\Requests\TransactionRequest;

use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Input;

class GuzzleController extends Controller
{   

    public function showForm()
    {
        return view('form');
    }

    public function postForm()
    {
        dd(Input::all());
    }



    public function getRemoteData(){
        $client = new Client([
                'headers' => ['Content-Type' =>'application/json' , 'Accept' => 'application/json' , 'Authorization' => 'Bearer --->TOKEN_ID<---'],
        ]);

        $request = $client->get('http:MyTestSite/api/--->LINK_ID<---');
        $response = $request->getBody()->getContents();
        echo '<pre>';
        print_r($response);
        exit;


    }

}



As you can see I put some arrows pointing out the TOKEN_ID and LINK_ID variables I need passed in from the Form submissions POST request that is being sent to my site.

I'm sure this is simple, and I'm just a derp. But I can't seem to find a post of somebody doing this exact task.

Thanks for the help.

0 likes
2 replies
Cronix's avatar
Cronix
Best Answer
Level 67

For forms that use a method other than get, you need to include the csrf token.

<form action="http://mysite/test" method="POST">
    {{ csrf_field() }}
    <input type='hidden' name='LINK_ID' value='test123'>
    <input type='hidden' name='TOKEN_ID' value='token123'>

    <input type="submit">
</form>

Assuming your form posts to the getRemoteData() method, you'd use the Request object to get the post data.

public function getRemoteData(Request $request){

    $linkId = $request->LINK_ID;
    $tokenId = $request->TOKEN_ID;

    // rest of your code using $linkId and $tokenId
}

https://laravel.com/docs/5.6/requests

jlrdw's avatar

There goes those Docs again, amazing what's covered there.

Please or to participate in this conversation.