PHP 实现appStore内购的服务器端验证


    /**
     * 随着苹果系统越来越强大,有种马上要开始胡来的节奏,个人认为强制添加内购就是其中之一,虽然很多人都特别鄙视这种行为,然并卵。
     * 具体的官方给出的验证规则,大家可以详细阅读看看:http://zengwu3915.blog.163.com/blog/static/2783489720137605156966/?suggestedreading
     * apple官方提供的文档地址:https://developer.apple.com/library/prerelease/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
     **/
    $apple_receipt = $this->_post('apple_receipt'); //苹果内购的验证收据,由客户端传过来
    $jsonData = array('receipt-data'=>$apple_receipt);//这里本来是需要base64加密的,我这里没有加密的原因是客户端返回服务器端之前,已经作加密处理
    $jsonData = json_encode($jsonData);
    //$url = 'https://buy.itunes.apple.com/verifyReceipt';  正式验证地址
    $url = 'https://sandbox.itunes.apple.com/verifyReceipt'; //测试验证地址
    $response = http_post_data($url,$jsonData);
    if($response->{'status'} == 0){
        echo '验证成功';
    }else{
        echo '验证失败'.$response->{'status'};
    }
//curl请求苹果app_store验证地址
function http_post_data($url, $data_string) {
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL, $url);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_handle,CURLOPT_HEADER, 0);
    curl_setopt($curl_handle,CURLOPT_POST, true);
    curl_setopt($curl_handle,CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($curl_handle,CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl_handle,CURLOPT_SSL_VERIFYPEER, 0);
    $response_json =curl_exec($curl_handle);
    $response =json_decode($response_json);
    curl_close($curl_handle);
    return $response;
}
?>

你可能感兴趣的:(PHP)