0

I'm getting error : Missing or incorrectly formatted payload

I'm generating device token from Apple DeviceCheck API in swift language with real device and also passing Transaction ID to this php api.

jwt token generating successfully with this code but further code not working with apple query bit api.

This is my server side code in 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;

 ?>

Where is problem in this code? Or any other solution in php code.

Is there anything that I'm missing.

Baljinder
  • 43
  • 1
  • 7

1 Answers1

0

I just checked the Docs. They don't want POST fields like a form, they want a JSON body instead. Here's a snippet of code that you should be able to tweak to do what you require:

$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);

Actually, looking again at your code, it could be as simple as running json_encode() on your array.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39