Convert php curl to GAE urlfetch for iTunes InApp verifyReceipt

我的梦境 提交于 2019-12-22 08:12:50

问题


Can someone help to convert this PHP Curl to UrlFetch ? This is used for Apple iTunes verifyReceipt

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 good 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 is able to post but returns this response XXX result:{"status":21002}


回答1:


Why do you json_decode $post_data in your urlfetch code but not in curl?




回答2:


I had an error like this, it turned out 'POST' had to be in uppercase when running locally. On .appspot it worked with lowercase 'post' but not locally on my PC.



来源:https://stackoverflow.com/questions/22775207/convert-php-curl-to-gae-urlfetch-for-itunes-inapp-verifyreceipt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!