You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过GuzzleRequest发起POST请求并配置API授权头?

问题:为POST请求实例化GuzzleRequest并配置请求体与授权头

正在配置一款API,需要实例化GuzzleRequest来提交POST请求体数据,同时生成API授权头。使用GuzzleRequest的原因是buildAuthorizationHeader($request)方法仅接受RequestInterface类型参数,此前GET请求的配置可正常运行,但POST请求的请求体配置存在问题。

当前的GuzzleRequest实例

$request = new GuzzleRequest(
    method: 'POST',
    uri: $url,
    //body:null,
);

需要传入的请求体(JSON结构)

body参数支持string|resources|null|StreamInterface类型,需传入以下数据:

{
    "payItemId": "S-112-948-MTNMOMO-20052-200040001-1",
    "amount": 1000
}

完整的POST请求函数代码

function headerauthorization()
{

    $token = "716c3fcf-98bd-435e-a16d-befc3b96da35";
    $secret = "9f96a7ed-338f-4609-83f8-27ce60fe87f4";
    //$url = "https://s3p.smobilpay.staging.maviance.info/v2/cashin?serviceid=20052";
    $url = "https://s3p.smobilpay.staging.maviance.info/v2/quotestd";

    $config = new \Maviance\S3PApiClient\Configuration();
    $config->setHost($url);
    $client = new \Maviance\S3PApiClient\ApiClient($token, $secret, ['verify' => false]);


    $request = new GuzzleRequest(
        method: 'POST',
        uri: $url,
        //body:null, 
    );

    $curl = curl_init();
    curl_setopt_array(
        $curl,
        array(
            CURLOPT_URL => 'https://s3p.smobilpay.staging.maviance.info/v2/quotestd',
            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_HTTPHEADER => array(
                'Authorization:' . $client->buildAuthorizationHeader($request) . ''
            ),
        )
    );
    $response = curl_exec($curl);
    curl_close($curl);
    $response = json_decode($response, true);

}

可正常运行的GET请求代码(参考)

可正常运行的GET请求代码

function headerauthorization()
{

    $token = "716c3fcf-98bd-435e-a16d-befc3b96da35";
    $secret = "9f96a7ed-338f-4609-83f8-27ce60fe87f4";
    $url = "https://s3p.smobilpay.staging.maviance.info/v2/cashin?serviceid=20052";
   

    $config = new \Maviance\S3PApiClient\Configuration();
    $config->setHost($url);
    $client = new \Maviance\S3PApiClient\ApiClient($token, $secret, ['verify' => false]);
    
    
    $request = new GuzzleRequest(
        method: 'GET',
        uri: $url,
        // headers: [],
        // body: null,
    );   
    $curl = curl_init();
    curl_setopt_array($curl, array(
       CURLOPT_URL => 'https://s3p.smobilpay.staging.maviance.info/v2/cashin?serviceid=20052',
       CURLOPT_RETURNTRANSFER => true,
       CURLOPT_ENCODING => '',
       CURLOPT_MAXREDIRS => 10,
       CURLOPT_TIMEOUT => 0,
       CURLOPT_FOLLOWLOCATION => true,
       CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
       CURLOPT_CUSTOMREQUEST => 'GET',
       CURLOPT_HTTPHEADER => array(
            'Authorization:'.$client->buildAuthorizationHeader($request).''
        ),
    )
    );
    $response = curl_exec($curl);
    curl_close($curl);
    $response = json_decode($response, true);
    $payItemId = $response[0]["payItemId"];
   
} 

现需解决POST请求中配置请求体的问题。


内容的提问来源于stack exchange,提问作者bricend96

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 20:04:53