php - 验证交易收据时的状态 21002
<p><p>我尝试查看所有其他问题,但似乎没有任何解决此问题的方法。我已经缩小范围,所以来自 <a href="https://sandbox.itunes.apple.com/verifyReceipt" rel="noreferrer noopener nofollow">https://sandbox.itunes.apple.com/verifyReceipt</a> 的响应只是 <code>{"status":21002}</code>。任何帮助将不胜感激,我已经复制了下面的相关代码。</p>
<pre><code>NSString *completeString = @"http://www.mysite.com/verify.php";
NSURL *urlForValidation = ;
NSMutableURLRequest *validationRequest = [ initWithURL:urlForValidation];
;
NSString *strTest = ];
];
NSData *responseData = ;
NSString *response = [ initWithData: responseData encoding: NSUTF8StringEncoding];
NSLog(@"%@", response);
- (NSString*) createEncodedString:(NSData*)data {
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const int size = ((data.length + 2)/3)*4;
uint8_t output;
const uint8_t* input = (const uint8_t*);
for (int i = 0; i < data.length; i += 3)
{
int value = 0;
for (int j = i; j < (i + 3); j++)
{
value <<= 8;
if (j < data.length)
value |= (0xFF & input);
}
const int index = (i / 3) * 4;
output =table[(value >> 18) & 0x3F];
output =table[(value >> 12) & 0x3F];
output = (i + 1) < data.length ? table[(value >> 6)& 0x3F] : '=';
output = (i + 2) < data.length ? table[(value >> 0)& 0x3F] : '=';
}
return[ initWithBytes:output length:size encoding:NSASCIIStringEncoding];
}
</code></pre>
<p>最后这是使用的PHP代码:</p>
<pre><code>$url = 'https://sandbox.itunes.apple.com/verifyReceipt';
$receipt = "{%s}" % $_POST["receipt"];
$purchase_encoded = base64_encode( $receipt );
$encodedData = json_encode( Array(
'receipt-data' => $purchase_encoded
) );
//Open a Connection using POST method, as it is required to use POST method.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData);
$encodedResponse = curl_exec($ch);
curl_close($ch);
//Decode response data using json_decode method to get an object.
echo $encodedResponse;
$response = json_decode( $encodedResponse );
echo $response;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>经过一番折腾,我终于弄明白了。我相信最大的错误在于我如何设置 HTTP 正文。最终代码如下,希望这可以帮助其他人!</p>
<p> Objective-C :</p>
<pre><code>NSString *completeString = @"http://www.mysite.com/verify.php";
NSURL *urlForValidation = ;
NSMutableURLRequest *validationRequest = [ initWithURL:urlForValidation];
;
NSString *strTest = ];
];
NSData *responseData = ;
NSString *response = [ initWithData: responseData encoding: NSUTF8StringEncoding];
NSLog(@"%@", response);
</code></pre>
<p>PHP:</p>
<pre><code>$url = 'https://sandbox.itunes.apple.com/verifyReceipt';
$encodedData = json_encode( Array(
'receipt-data' => $_POST["receipt"]
) );
//Open a Connection using POST method, as it is required to use POST method.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData);
$encodedResponse = curl_exec($ch);
curl_close($ch);
$response = json_decode( $encodedResponse );
/*
echo "Status Code:";
echo $response->{'status'};
*/
if ($response->{'status'} != 0) {
echo "FAIL";
} else {
echo "SUCCESS";
}
</code></pre></p>
<p style="font-size: 20px;">关于php - 验证交易收据时的状态 21002,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/12964728/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/12964728/
</a>
</p>
页:
[1]