Laravel 4 make post request from controller to external url with data

后端 未结 3 1135
一向
一向 2021-01-04 11:03

I am looking for a way to make a post request from a controller to an external url. The data being posted is a php array. The url to recieve is an ecommerce API in an extern

3条回答
  •  走了就别回头了
    2021-01-04 11:46

    We can use package Guzzle in Laravel, it is a PHP HTTP client to send HTTP requests.

    You can install Guzzle through composer

    composer require guzzlehttp/guzzle:~6.0
    

    Or you can specify Guzzle as a dependency in your project's existing composer.json

    {
       "require": {
          "guzzlehttp/guzzle": "~6.0"
       }
    }
    

    Example code of POST Request in laravel, using Guzzle is as shown below,

    use GuzzleHttp\Client;
    class yourController extends Controller {
    public function saveApiData()
    {
        $client = new Client();
        $res = $client->request('POST', 'https://url_to_the_api', [
            'form_params' => [
                'client_id' => 'test_id',
                'secret' => 'test_secret',
            ]
        ]);
    
        $result= $res->getBody();
        dd($result);
    

    }

提交回复
热议问题