关于 PHP curl 请求 header 的问题。困扰了我一整天了!

2021-06-29 09:49:55 +08:00
 madpecker009

我们调用的是大华的接口,php 和 Java 生成的 HMAC-SHA256 签名是一样的。Java 请求就返回 200,用 php 请求该接口就一直是 401 未授权。所以说生成的签名不可能有问题。昨天晚上一直在找问题,所获请求不成功。话不多说,直接上代码!

<?php
	$contentMD5 = "";
    $contentType = "application/json";
    $xlcNonce = create_uuid();
    $date1 = date("Ymd\THis\Z"); //20210626T111514Z
    $ak='1233444';
    $SK = "1234566677";
    $httpMethod_name = "GET"; // 两个值 GET POST
    $CanonicalizedResource = "/eags/api/fsds/v1.0/school-roll/search?id=1&name=12&pId=123&level=0";
    $stringToSign = $httpMethod_name."\n".$contentMD5."\n".$contentType."\n".$date1."\n".$xlcNonce."\n"."1.0"."\n".$CanonicalizedResource;
    echo "要加密的数据-------------\n\n\n".$stringToSign;
    echo "\n\n\n\n\n";
    $hash_code = base64_encode(hash_hmac("sha256", $stringToSign, $SK, true));
    echo 'hashCode-------------'.$hash_code;
    echo "\n\n\n\n\n";
    // headers 信息
    $headers = [
        "Authorization:LC-HMAC-SHA256 ".$ak.":".$hash_code,
        "Content-MD5:".$contentMD5,
        "Content-type:".$contentType,
        "X-LC-Date: ".$date1,
        "X-LC-Nonce: ".$xlcNonce, // 生成 hash 时候的 uuid
        "X-LC-VERSION: 1.0",
        "accept:*/*",
        "connection:Keep-Alive",
        "user-agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"
    ];

    function create_uuid($prefix = ""){
        $str = md5(uniqid(mt_rand(), true));
        $uuid  = substr($str,0,8) . '-';
        $uuid .= substr($str,8,4) . '-';
        $uuid .= substr($str,12,4) . '-';
        $uuid .= substr($str,16,4) . '-';
        $uuid .= substr($str,20,12);
        return $prefix . $uuid;
    }

    function get2($url, $header){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPGET, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //TRUE 将 curl_exec()获取的信息以字符串返回,而不是直接输出。

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        print_r($header);
        curl_setopt($ch, CURLOPT_HEADER, true); //返回 response 头部信息
        curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求 header
        //禁止 https 协议验证 ssl 安全认证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        $result = curl_exec($ch);
        echo "\n\n\n\n\n";
        echo curl_getinfo($ch, CURLINFO_HEADER_OUT); //官方文档描述是“发送请求的字符串”,其实就是请求的 header 。这个就是直接查看请求 header,因为上面允许查看
        curl_close($ch);
        return $result;
    }
    echo get2("http://XXX.XXX.Xxx.211/eags/api/fsds/v1.0/school-roll/search?id=1&name=12&pId=123&level=0", $headers);
下图是打印的 php 的 header 。

1185 次点击
所在节点    PHP
2 条回复
czhuyu
2021-07-09 11:28:36 +08:00
可以抓包对比下看看
yqf0215
2021-08-01 18:15:25 +08:00
$stringToSign 、hash_hmac("sha256", $stringToSign....)这两个值,与 java 生成的两个值,都一样吗?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/786391

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX