简体   繁体   中英

php script for in-app purchase

i'm doing an application for iPhone/iPad which uses the in-app purchase functionality. On the server side i load a php script for the receipt verification. Here is the content of the script:

<?php
$receipt = json_encode(array("receipt-data" => $_GET["receipt"]));
// NOTE: use "buy" vs "sandbox" in production.
$url = "https://sandbox.itunes.apple.com/verifyReceipt";
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_HEADER, 0);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $receipt); 
$response_json = curl_exec($curl_handle);
$response = json_decode($response_json);
curl_close($curl_handle);
print $response->{'status'};
?>

My problem is that the response variable is always empty, i can't receive any response. Also the response_json is empty and i can't understand which is the problem. I think is something server side, but i'm not an expert of php and the server is not under my direct control. Can anyone suggest me a way to resolve the problem or to test which coulod be?

Thank's

If curl_exec() fails, it will return a boolean FALSE. You blindly feed the response to json_decode and assume you'll get something out. Decoding boolean false gives you an empty var. Instead, try this:

...
$response_json = curl_exec($curl_handle);
if ($response_json === FALSE) {
    die(curl_error($curl_handle));
}
$response = json_decode($response_json);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM