问下这个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] => 签名错误

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

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

试试这样:

$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();
2个月前 评论
donggan (楼主) 2个月前
忆往昔弹指间 (作者) 2个月前
lovewei 2个月前
忆往昔弹指间 (作者) 2个月前
讨论数量: 11

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

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

试试这样:

$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();
2个月前 评论
donggan (楼主) 2个月前
忆往昔弹指间 (作者) 2个月前
lovewei 2个月前
忆往昔弹指间 (作者) 2个月前

laravel底层用的是也是guzzle

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

file

2个月前 评论

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