如何使用 guzzle post 方法发送变量
How to send variable with the guzzle post method
我有一个 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",
}'
]);
一切正常,正在发送静态数据,但我想知道如何发送变量。
您可以在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",
]
]);
希望这对你有用。
Amount 可以传入一个数组,然后你可以使用 json 使用```json_encode``
进行编码
希望这对你有用。
$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);
]);
您可以尝试使用 Guzzle json 选项:
$amount = $request->get('amount');
$client = new \GuzzleHttp\Client();
$response = $client->post(
'http://192.168.150.16:7585/api/v1/Transaction/GetTransactionNumber',
[
GuzzleHttp\RequestOptions::JSON => [
'Amount' => $amount,
'something' => '1',
'Description' => 'desc',
]
]
);
查看 Guzzle 手册 - http://docs.guzzlephp.org/en/stable/request-options.html#json
我有一个 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",
}'
]);
一切正常,正在发送静态数据,但我想知道如何发送变量。
您可以在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",
]
]);
希望这对你有用。
Amount 可以传入一个数组,然后你可以使用 json 使用```json_encode``
进行编码希望这对你有用。
$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);
]);
您可以尝试使用 Guzzle json 选项:
$amount = $request->get('amount');
$client = new \GuzzleHttp\Client();
$response = $client->post(
'http://192.168.150.16:7585/api/v1/Transaction/GetTransactionNumber',
[
GuzzleHttp\RequestOptions::JSON => [
'Amount' => $amount,
'something' => '1',
'Description' => 'desc',
]
]
);
查看 Guzzle 手册 - http://docs.guzzlephp.org/en/stable/request-options.html#json