CloudKit服务器到服务器和PHP

时间:2017-03-02 21:16:38

标签: php cloudkit cloudkit-web-services server-to-server

以下PHP代码在开发环境中完美运行但在生产环境中返回错误:

{“uuid”:“xxxxx”,“serverErrorCode”:“AUTHENTICATION_FAILED”,“reason”:“身份验证失败”}

任何想法?

function cloudKitRequest($requestType, $jsonbody)
{
    $KEY_ID                 = '97659598759875987508750875087087608785987585985';
    $CONTAINER              = 'xyz.zz.zzyyxx.xyz';
    $PRIVATE_PEM_LOCATION   = 'eckey.pem';
    //$Environment            = 'development';
    $Environment            = 'production';
    $dbtype                 = 'public';
    $result                 = NULL;


    $url = 'https://api.apple-cloudkit.com/database/1/' . $CONTAINER . '/'. $Environment . '/' . $dbtype . '/records/'.$requestType;

    // Set cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);

    // Create signature
    date_default_timezone_set('UTC');
    $explode_date = explode('+', date("c", time()));
    $time = $explode_date[0] . 'Z';
    $signature = $time . ":" . base64_encode(hash("sha256", $jsonbody, true)) . ":" . explode('cloudkit.com', $url)[1];
    // Get private key
    $pkeyid = openssl_pkey_get_private("file://" . $PRIVATE_PEM_LOCATION);
    // Sign signature with private key
    if(openssl_sign($signature, $signed_signature, $pkeyid, "sha256WithRSAEncryption")) {
        openssl_free_key($pkeyid);
        // Set headers
        curl_setopt($ch, CURLOPT_HTTPHEADER,
                    [
                    "Content-Type: text/plain",
                    "X-Apple-CloudKit-Request-KeyID: " . $KEY_ID,
                    "X-Apple-CloudKit-Request-ISO8601Date: " . $time,
                    "X-Apple-CloudKit-Request-SignatureV1: " . base64_encode($signed_signature),
                    ]
                    );
        // Set body
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonbody);
        // Send the request & save response to $resp
        $resp = curl_exec($ch);
        if($resp === false) {
            die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
        } else {
            //echo $resp;
            // $result = json_decode($resp, true);
            $result = $resp;
        }
        curl_close($ch);
    } else {
        while ($msg = openssl_error_string()) {
            echo $msg . "<br />\n";
        }
    }
    return $result;
}

任何想法

0 个答案:

没有答案
相关问题