使用DeviceCheck API和服务器端php代码唯一标识iOS设备

时间:2019-07-18 11:37:46

标签: php ios swift devicecheck

我遇到错误:有效载荷丢失或格式错误

我正在从Apple DeviceCheck API生成具有真实设备的快速语言的设备令牌,并将事务ID传递给该php API。

jwt令牌已使用此代码成功生成,但进一步的代码不适用于Apple查询位api。

这是我在php中的服务器端代码:

<?php
require_once "vendor/autoload.php";
use Zenstruck\JWT\Token;
use Zenstruck\JWT\Signer\OpenSSL\ECDSA\ES256;
use \Ramsey\Uuid\Uuid;


$deviceToken = (isset($_POST["deviceToken"]) ? $_POST["deviceToken"] : null);
$transId = (isset($_POST["transId"]) ? $_POST["transId"] : null);

function generateJWT($teamId, $keyId, $privateKeyFilePath) {
    $payload = [
    "iss" => $teamId,
    "iat" => time()
    ];
    $header = [
    "kid" => $keyId
    ];
    $token = new Token($payload, $header);
    return (string)$token->sign(new ES256(), $privateKeyFilePath);
}

$teamId = "#####";// I'm passing My team id
$keyId = "#####"; // I'm passing my key id 
$privateKeyFilePath = "AuthKey_4AU5LJV3.p8";
$jwt = generateJWT($teamId, $keyId, $privateKeyFilePath);


function postReq($url, $jwt, $bodyArray) {

    $header = [
        "Authorization: Bearer ". $jwt
    ];



    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyArray);  //Post Fields
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

//        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    $server_output = curl_exec($ch);

    //$info = curl_getinfo($ch);
   // print_r($info);
    //echo 'http code: ' . $info['http_code'] . '<br />';
    //echo curl_error($ch);

    curl_close($ch);

    return  $server_output;


}


$body = [
"device_token" => $deviceToken,
"transaction_id" => $transId,
"timestamp" => ceil(microtime(true)*1000)
];

$myjsonis = postReq("https://api.development.devicecheck.apple.com/v1/query_two_bits", $jwt, $body);

echo $myjsonis;

 ?>

此代码中的问题在哪里?或任何其他php代码解决方案。

有什么我想念的吗?

1 个答案:

答案 0 :(得分:0)

我刚刚检查了文档。他们不需要像表单那样的POST字段,而是想要JSON正文。这是一段代码,您应该可以对其进行调整以执行所需的操作:

$data = array("name" => "delboy1978uk", "age" => "41");                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://api.local/rest/users');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

实际上,再次查看您的代码,就像在数组上运行json_encode()一样简单。