已达到http/2配置,只是openssl没有达到1.0.2e该版本,但是我用到的是Authentication Tokens,没有用到证书。每次请求apns返回的报错:HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: 504f5354202f332f6465766963652f646132323735306338。
我用php语言写的,下面是代码:
$PayLoad = array("iss"=>"XXXXXX","iat"=>time());
$KidID = "XXXXXXX";
$Key = "XXXXXX...........";
$Token = $this->GetToken($PayLoad,$Key,"ES256",$KidID);
$verify_url = "https://api.development.push.apple.com/3/device/XXXX.....";
$content['aps'] = array("alert"=>"Hello");
$HTTP_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36";
$BundleID = "com.XXX.XXX";
$HEADERArr = array('apns-topic: '.$BundleID,'Authorization: bearer ' . $Token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $verify_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); //
curl_setopt($ch, CURLOPT_PORT,443);
curl_setopt($ch, CURLOPT_USERAGENT, $HTTP_USER_AGENT); //
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //
curl_setopt($ch, CURLOPT_AUTOREFERER, true); //
curl_setopt($ch, CURLOPT_POST, true); //
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content,JSON_UNESCAPED_UNICODE)); //
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER,$HEADERArr);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //
curl_setopt($ch, CURLOPT_HEADER, true); //
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //
$output = curl_exec($ch); // 执行操作
if(curl_errno($ch))
{
echo "Errno".curl_error($ch)."<br />"; // 捕抓异常
}
curl_close($ch); // 关闭CURL
var_dump($output);
function GetToken($Payload,$Key, $alg = 'ES256', $KidID = null, $Head = null)
{
$Header = array('typ' => 'JWT', 'alg' => $alg);
if ($KidID !== null) {
$Header['kid'] = $KidID;
}
if ( isset($Head) && is_array($Head) ) {
$Header = array_merge($Head,$Header);
}
$HeaderJson = json_encode($Header);
$PayloadJson = json_encode($Payload);
$Segments = array();
$Segments[] = $this->UrlSafeB64Encode($HeaderJson);
$Segments[] = $this->UrlSafeB64Encode($PayloadJson);
$SigningInput = implode('.',$Segments);
$Signature = $this->sign($SigningInput,$Key);
$Segments[] = $this->UrlSafeB64Encode($Signature);
return implode('.',$Segments);
}
function UrlSafeB64Encode($Input)
{
return str_replace('=', '', strtr(base64_encode($Input), '+/', '-_'));
}
function sign($Msg,$Key)
{
return hash_hmac('SHA256',$Msg,$Key,true);
}
}
不知道是否和OpenSSL有关,请大神看下。