Convert php curl to GAE urlfetch for iTunes InApp verifyReceipt

Can anyone help convert this PHP Curl to UrlFetch? This is used to check Apple iTunesReceipt

if (getiTunesProductionLevel($app_id)=="sandbox" || $sandbox_override == TRUE) {
    $endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';
}
else {
    $endpoint = 'https://buy.itunes.apple.com/verifyReceipt';
}

$postData = json_encode(array(
    'receipt-data' => $receipt,
    'password' => $sharedSecret));

$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);

This is as good as I can get. But not enough.

logMessage(LogType::Info,"XXX URLFetch 0");
    $postData = json_encode(array(
    'receipt-data' => $receipt,
    'password' => $sharedSecret));
$post_data = json_decode($postData);
logMessage(LogType::Info,"XXX URLFetch 1");
$data = http_build_query($post_data);
logMessage(LogType::Info,"XXX URLFetch 2");

$context = [
  'http' => [
    'method' => 'post',
    'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
    'content' => $data
  ]
];

logMessage(LogType::Info,"XXX URLFetch 3");
$context = stream_context_create($context);
logMessage(LogType::Info,"XXX URLFetch 4");
$result = file_get_contents($endpoint, false, $context);
logMessage(LogType::Info,"XXX result:" . $result);
$response = $result;
$errno = 0;
logMessage(LogType::Info,"XXX response:");

It can publish, but returns this answer XXX result: {"status": 21002}

+4
source share
2 answers

Why are you using json_decode $ post_data in urlfetch code but not in curl?

+1
source

: , "POST" . .appspot "", .

0

Source: https://habr.com/ru/post/1534346/


All Articles