如何使用枪口发布方法发送变量

问题描述 投票:2回答:3

我有一个API,我需要向其中发送一些数据,并且我正在使用guzzle对其进行处理,因此这是我的代码:

$amount = $request->get('amount');
    $client = new \GuzzleHttp\Client();
    $requestapi = $client->post('http://192.168.150.16:7585/api/v1/Transaction/GetTransactionNumber', [
        'headers' => ['Content-Type' => 'application/json'],
        'body' => '{
        "Amount":"i want to send $amount here",
        "something":"1",
        "Description":"desc",
        }'
    ]);

所以一切都很好,正在发送静态数据,但我想知道如何发送变量。

php laravel guzzle
3个回答
1
投票

金额可以传入数组,并且可以使用```json_encode``进行json编码后

希望这对您有用。

$amount = $request->get('amount');
$client = new \GuzzleHttp\Client();
$url   = "http://192.168.150.16:7585/api/v1/Transaction/GetTransactionNumber";
$data   = [
            "amount"      => $amount,
            "something"   => "1",
            "description" => "desc",
          ];

$requestAPI = $client->post( $url, [
        'headers' => ['Content-Type' => 'application/json'],
        'body' => json_encode($data);
    ]);

4
投票

您可以像这样在form_params参数中绑定数据>

$client = new \GuzzleHttp\Client();
$amount = $request->get('amount');
$requestapi = $client->post('http://192.168.150.16:7585/api/v1/Transaction/GetTransactionNumber', [
                   'form_params' => [
                        "Amount" => "i want to send $amount here",
                        "something" => "1",
                        "Description" => "desc",
                   ]
             ]);

希望这对您有用。


0
投票

您可以尝试使用Guzzle json

© www.soinside.com 2019 - 2024. All rights reserved.