Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5401236
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: PHP

2013-05-06 16:15:25

PHP 服务器端代码,

首先要确  php的 curl 和  SSL (open_ssl)这两个模块开启,可以在  php.ini  中去掉  这两个dll前面的分号。



  1.     //服务器二次验证代码  
  2.     function getReceiptData($receipt$isSandbox = false)     
  3.     {     
  4.         if ($isSandbox) {     
  5.             $endpoint = '';     
  6.         }     
  7.         else {     
  8.             $endpoint = '';     
  9.         }     
  10.       
  11.         $postData = json_encode(     
  12.             array('receipt-data' => $receipt)     
  13.         );     
  14.       
  15.         $ch = curl_init($endpoint);     
  16.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     
  17.         curl_setopt($ch, CURLOPT_POST, true);     
  18.         curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);     
  19.        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);  //这两行一定要加,不加会报SSL 错误  
  20.         curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);   
  21.   
  22.   
  23.         $response = curl_exec($ch);     
  24.         $errno    = curl_errno($ch);     
  25.         $errmsg   = curl_error($ch);     
  26.         curl_close($ch);     
  27.     //判断时候出错,抛出异常  
  28.         if ($errno != 0) {     
  29.             throw new Exception($errmsg$errno);     
  30.         }     
  31.                   
  32.         $data = json_decode($response);     
  33.     //判断返回的数据是否是对象  
  34.         if (!is_object($data)) {     
  35.             throw new Exception('Invalid response data');     
  36.         }     
  37.     //判断购买时候成功  
  38.         if (!isset($data->status) || $data->status != 0) {     
  39.             throw new Exception('Invalid receipt');     
  40.         }     
  41.       
  42.     //返回产品的信息             
  43.         return array(     
  44.             'quantity'       =>  $data->receipt->quantity,     
  45.             'product_id'     =>  $data->receipt->product_id,     
  46.             'transaction_id' =>  $data->receipt->transaction_id,     
  47.             'purchase_date'  =>  $data->receipt->purchase_date,     
  48.             'app_item_id'    =>  $data->receipt->app_item_id,     
  49.             'bid'            =>  $data->receipt->bid,     
  50.             'bvrs'           =>  $data->receipt->bvrs     
  51.         );     
  52.     }     
  53.       
  54.     //获取 App 发送过来的数据,设置时候是沙盒状态  
  55.         $receipt   = $_GET['data'];     
  56.         $isSandbox = true;     
  57.     //开始执行验证  
  58.     try  
  59.      {  
  60.          $info = getReceiptData($receipt$isSandbox);     
  61.          // 通过product_id 来判断是下载哪个资源  
  62.          switch($info['product_id']){  
  63.             case 'com.application.xxxxx.xxxx':  
  64.                 Header("Location:xxxx.zip");  
  65.             break;             
  66.         }  
  67.      }  
  68.     //捕获异常  
  69.     catch(Exception $e)  
  70.     {  
  71.         echo 'Message: ' .$e->getMessage();  
  72.     }  
  73. ?>  
阅读(2187) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~