Created
September 21, 2016 05:53
-
-
Save schnabear/8bbe1c1f5fff430cad3b26697ab80b6d to your computer and use it in GitHub Desktop.
PHP Apple Receipt Verification Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http://stackoverflow.com/questions/19785803/validating-appreceiptstoreurl-returning-21002-status | |
// http://stackoverflow.com/questions/19222845/ios7-receipts-not-validating-at-sandbox-error-21002-java-lang-illegalargume | |
function verify_curl($base64receipt, $sandbox = false) | |
{ | |
if ($sandbox) { | |
$url = 'https://sandbox.itunes.apple.com/verifyReceipt'; | |
} else { | |
$url = 'https://buy.itunes.apple.com/verifyReceipt'; | |
} | |
$data = json_encode(array('receipt-data' => $base64receipt)); | |
// 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, $data); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return json_decode($result, true); | |
} | |
function verify_hrequest($base64receipt, $sandbox = false) | |
{ | |
$data = '{"receipt-data" : "'.$base64receipt.'"}'; | |
if ($sandbox) { | |
$request = new HttpRequest('https://sandbox.itunes.apple.com/verifyReceipt', HTTP_METH_POST); | |
} else { | |
$request = new HttpRequest('https://buy.itunes.apple.com/verifyReceipt', HTTP_METH_POST); | |
} | |
$request->setBody($data); | |
$request->send(); | |
$result = $request->getResponseBody(); | |
return json_decode($result, true); | |
} | |
function verify_fgcontents($base64receipt, $sandbox = false) | |
{ | |
if ($sandbox) { | |
$url = 'https://sandbox.itunes.apple.com/verifyReceipt'; | |
} else { | |
$url = 'https://buy.itunes.apple.com/verifyReceipt'; | |
} | |
$data = json_encode(array("receipt-data" => $base64receipt)); | |
// Use key 'http' even if you send the request to https://... | |
// This: 'content' => http_build_query($data), seems to generate an error (21002) | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/x-www-form-urlencoded", | |
'method' => 'POST', | |
'content' => $data | |
), | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
return json_decode($result, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment