gpt4 book ai didi

php - 如何使用正文和标题发出 GuzzleHttp 发布请求?

转载 作者:行者123 更新时间:2023-12-04 07:20:58 24 4
gpt4 key购买 nike

我无法理解如何修改此 cURL进入 Laravel 5.8,得到 {"code":"11", "message":"invalid Request found", "status":"DECLINED"}回复。
这是我的 cURL工作正常的代码(在 postman 和浏览器中):

$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.hylo.biz/Api/v1.0/Payment',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"orderId":"KJDHKSGIU768",
"amount":"100",
"redirect_url":"google.com"
}',
CURLOPT_HTTPHEADER => [
'Authorization: Basic WUR1pYQ1hYY3U2Og2OGM6JJE5tWEDJhJDEwZReVE=',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);

echo $response;
这是我的 Laravel 代码:
public function process(Request $request)
{
// return $request['request'];
$client = new \GuzzleHttp\Client();
$url = "https://api.hylo.biz/Api/v1.0/Payment";
$response = $client->request('POST', $url, [
'headers' => [
'Authorization' => 'Basic WUR1pYQ1hYY3U2Og2OGM6JJE5tWEDJhJDEwZReVE=',
'Content-Type' => 'application/json'
],
'form_params' => $request['request']
]);

return $response = $response->getBody();
}

最佳答案

in this way it's working

public function process(Request $request)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.hylo.biz/Api/v1.0/Payment',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"orderId":"KJDHKSGIU768",
"amount":"100",
"redirect_url":"google.com"
}',
CURLOPT_HTTPHEADER => [
'Authorization: Basic WUR1pYQ1hYY3U2Og2OGM6JJE5tWEDJhJDEwZReVE=',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);
}

and also I forgot to add use Illuminate\Http\Request;

关于php - 如何使用正文和标题发出 GuzzleHttp 发布请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68504406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com