问下这个postman对应的php代码如何转换成 HTTP形式?

postman截图

问下这个postman对应的php代码如何转换成 HTTP形式?

curl_setopt_array($curl, array(
            CURLOPT_URL => 'http://192.168.1.231/v1/api/pubsub/busi/record/query',
            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 =>'{}',
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
                'cappkey: appkey1',
                'cnonce: 123456',
                'ctimestamp: 1707187818264',
                'csign: a8e05c6c4d19841783a241c110da6e9e'
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);
        echo $response;
这代码是能返回数据的

然后我想把这端代码改成

use Illuminate\Support\Facades\Http;
$request_header = [
            'Content-Type'=>'application/json',
            'cappkey'=>'appkey1',
            'cnonce'=>'123456',
            'ctimestamp'=>'1707187818264',
            'csign'=>'a8e05c6c4d19841783a241c110da6e9e'
        ];
$response = Http::withHeaders($request_header)->withOptions(["verify"=>false])
                    ->asJson()
                    ->timeout(3)
                    ->post('http://192.168.1.231/v1/api/pubsub/busi/record/query',[])->body();
但是我这么写法就报
 [code] => 200008
 [msg] => 签名错误

帮忙看下我写法哪里有问题了?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

试试这样:

$request_header = [
    'Content-Type'=>'application/json',
    'cappkey'=>'appkey1',
    'cnonce'=>'123456',
    'ctimestamp'=>'1707187818264',
    'csign'=>'a8e05c6c4d19841783a241c110da6e9e'
];
$response = Http::withHeaders($request_header)->withOptions(["verify"=>false])
    ->timeout(3)
    ->withBody('{}')
    ->post('http://192.168.1.231/v1/api/pubsub/busi/record/query')->body();

或者这样:

$request_header = [
    'Content-Type'=>'application/json',
    'cappkey'=>'appkey1',
    'cnonce'=>'123456',
    'ctimestamp'=>'1707187818264',
    'csign'=>'a8e05c6c4d19841783a241c110da6e9e'
];
$response = Http::withHeaders($request_header)->withOptions(["verify"=>false])
    ->asJson()
    ->timeout(3)
    ->post('http://192.168.1.231/v1/api/pubsub/busi/record/query', new \stdClass())->body();
3个月前 评论
donggan (楼主) 3个月前
忆往昔弹指间 (作者) 3个月前
lovewei 3个月前
忆往昔弹指间 (作者) 3个月前
讨论数量: 11

试试这样:

$request_header = [
    'Content-Type'=>'application/json',
    'cappkey'=>'appkey1',
    'cnonce'=>'123456',
    'ctimestamp'=>'1707187818264',
    'csign'=>'a8e05c6c4d19841783a241c110da6e9e'
];
$response = Http::withHeaders($request_header)->withOptions(["verify"=>false])
    ->timeout(3)
    ->withBody('{}')
    ->post('http://192.168.1.231/v1/api/pubsub/busi/record/query')->body();

或者这样:

$request_header = [
    'Content-Type'=>'application/json',
    'cappkey'=>'appkey1',
    'cnonce'=>'123456',
    'ctimestamp'=>'1707187818264',
    'csign'=>'a8e05c6c4d19841783a241c110da6e9e'
];
$response = Http::withHeaders($request_header)->withOptions(["verify"=>false])
    ->asJson()
    ->timeout(3)
    ->post('http://192.168.1.231/v1/api/pubsub/busi/record/query', new \stdClass())->body();
3个月前 评论
donggan (楼主) 3个月前
忆往昔弹指间 (作者) 3个月前
lovewei 3个月前
忆往昔弹指间 (作者) 3个月前

laravel底层用的是也是guzzle

你直接用apifox,他生成的对应代码都有,复制就能用了。

file

3个月前 评论

目前已经有 Guzzle 了,Laravel HTTP 也是基于 Guzzle 封装的好像,如果一定要用 Laravel 的 HTTP,可以去 postmanlabs/postman-code-generators 这个仓库提个 PR。

3个月前 评论
donggan (楼主) 3个月前
sanders 3个月前
donggan (楼主) 3个月前
donggan (楼主) 3个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!