verifying apple receipt with PHP is not working sometime - php

I'm using below code to verify apple receipt using PHP, using CURL i'm calling apple server and trying to get response.This code is working fine but sometimes apple JSON response is coming empty and i'm not getting any error message also. It just goes blank.
Is this is the only method to verify apple receipt using PHP? or please correct me in my code what is the mistake I made because when I try to debug this i'm getting empty response but this issue is not all time if I send 10 request 7 are giving response and 3 are returning blank/empty.
Thanks.
function subscription_curl ($request)
{
ini_set('max_execution_time', 0);
$decodeResponse = "";
$appleurl = "https://buy.itunes.apple.com/verifyReceipt"; // for production
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $appleurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$encodedResponse = curl_exec($ch);//Encoded apple response
curl_close($ch);
$decodeResponse = json_decode($encodedResponse, TRUE);//Decoded apple response
$applestatus1 = $decodeResponse['status'];
if($applestatus1 == 21007)
{
$appleurl = "https://sandbox.itunes.apple.com/verifyReceipt"; // for sandbox
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $appleurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$encodedResponse = curl_exec($ch);//Encoded apple response
curl_close($ch);
$decodeResponse = json_decode($encodedResponse, TRUE);//Decoded apple response
}
return array($encodedResponse, $decodeResponse);
}
$decodeResponse1 = subscription_curl($request); // Call curl function to send request to apple
$encodedResponse = $decodeResponse1[0];
$decodeResponse = $decodeResponse1[1];
print_r($decodeResponse)

Add error checking might be something there,right after curl_exec
if (curl_errno($ch)) {
$error_message = curl_error($ch);
echo $error_message;
}

Related

Check response status code with PHP / cURL (file_get_contents_curl)

I use this function (from here) with cURL in PHP:
<?php
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
This works quite well - - for example, that way I can get the 'body' of a JSON. Like this:
<?php
$crossrefurl = 'https://api.crossref.org/works/10.1163/1871191X-13020004';
$obj = file_get_contents_curl($crossrefurl);
# convert to json
$obj = json_decode($obj, true);
What I don't know is: how can I get the response status code (given that my ´curl`-commands are in a function)?
Specifically, I want to find out whether the response status code is 429 in which case I would let the script sleep for 5 seconds (to avoid being rate-limited).
According to this post, the code should be something like:
<?php
if(($httpcode == 429)) { sleep(5); }
... but how do I get to $httpcode in the first place?
Thanks for your help!

Undefined constant http/2

I am trying to work out how to send push notifications to apple iOS devices from a php site. I had a php script that was working before the new http/2 way but since they do not support it anymore, it of course does not work.
The new script I have is this:
<?php
$ch = curl_init();
$device_token = 'correctDevice';
$pem_file = 'correctPemfile';
$pem_secret = 'correctCode';
$apns_topic = 'correctAppID';
//curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
curl_setopt($ch, CURLOPT_VERBOSE , true);
echo "Try 1 ================================================" . PHP_EOL;
//setup and send first push message
$url = "https://api.development.push.apple.com/3/device/$device_token";
curl_setopt($ch, CURLOPT_URL, "{$url}");
$sample_alert = '{"aps":{"alert":"hi #1","sound":"default"}}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch);
//var_dump($response);
//var_dump($httpcode);
echo "Try 2 ================================================" . PHP_EOL;
//setup and send second push message
$url = "https://api.development.push.apple.com/3/device/$device_token";
curl_setopt($ch, CURLOPT_URL, "{$url}");
$sample_alert = '{"aps":{"alert":"hi #2","sound":"default"}}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch);
//var_dump($response);
//var_dump($httpcode);
curl_close($ch);
?>
I've blocked out the device token, pem file, secret and app ID of course, but they are all correct.
The issue with it is that I get this error:
Warning: Use of undefined constant CURL_HTTP_VERSION_2_0 - assumed 'CURL_HTTP_VERSION_2_0' (this will throw an Error in a future version of PHP)
What could this issue be? I tried echoing phpinfo() and see if it supports http/2 and I found this:
From the documentation:
CURL_HTTP_VERSION_2_0 (int)
Available since cURL 7.33.0
Check the cURL version (e.g. by using phpinfo()).

how to integrate atom payment gateway in PHP

I am using ATOM payment gateway and YII framework. I am using following code & i am not getting response here $returnData = curl_exec($ch); its returning empty.
Please tell how can i come over it. is ther any tutorials for this integration.
$url = ‘http://203.114.240.77/paynetz/epi/fts';// test bed URL
$port = 80;
$atom_prod_id = “NSE”;
// code to generate token
$param = "&login=".$userid."&pass=".$password."&ttype=NBFundTransfer&prodid=".$atom_prod_id."&amt=".$amount."&txncurr=INR&txnscamt=0&clientcode=".$clientcode."&txnid=".$invoiceid."&date=".$today."&custacc=12345";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_PORT , $port);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
$returnData = curl_exec($ch);
// Check if any error occured
if(curl_errno($ch))
{
echo ‘Curl error: ‘ . curl_error($ch);
}
curl_close($ch);
$xmlObj = new SimpleXMLElement($returnData);
$final_url = $xmlObj->MERCHANT->RESPONSE->url;
// eof code to generate token
// code to generate form action
$param = “”;
$param .= “&ttype=NBFundTransfer”;
$param .= “&tempTxnId=”.$xmlObj->MERCHANT->RESPONSE->param[1];
$param .= “&token=”.$xmlObj->MERCHANT->RESPONSE->param[2];
$param .= “&txnStage=1″;
$url = $url.”?”.$param;
// eof code to generate form action
Please check for the http response code. Are you expecting any data to be returned? You're posting data, maybe the response-body should be empty?
Please add the code below to check for the http status code.
$info = curl_getinfo($ch);
echo $info["http_code"];

Toggl Reporting API with PHP cURL

I am trying to access the Toggl Reporting API.
I tried following in PHP with cURL, which connects to the API but gives the following error message: 'This method may not be used.' Any light on why this is the case would be useful as I'm very new to webservices. I may be missing something obvious or totally going the wrong way about it, so apologies if this is the case.
<?php
$userAgent = 'xxx';//username
$token = 'xxx';//token
$returned_content = get_data('https://toggl.com/reports/api/v2/summary?&workspace_id=[workspaceid]&since=2013-05-19&until=2013-05-20&user_agent=[username here]');
print_r($returned_content);
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERPWD, $token.':api_token');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
Edit: I tried a different approach. If I run the following code, I no longer receive any error messages, so the code seems to be executing but I can't print the response to the screen. Is there something specific I need to do to view the output other than print_r?(Toggl API returns JSON). Thanks.
$json = curl%20-v%20-u%[myapitoken]:api_token%20https://toggl.com/reports/api/v2/weekly?workspace_id=[id]&wsid=282507&since=2012-08-19&until=2013-09-20&user_agent=[user].json;
print_r($json);
Edit: Finally resolved! Code is as follows:
$workspace_id = '[id here]';
$user_agent = '[user agent here]'; // no spaces
$api_token = '[token here]';
$report_url = 'https://toggl.com/reports/api/v2/weekly?user_agent='.$user_agent.'&since=2013-08-01&until=2013-09-01';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_USERPWD, $api_token . ':api_token');
curl_setopt($ch, CURLOPT_URL, $report_url . '&workspace_id=' . $workspace_id);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
$result = json_encode($result);
Hope this helps someone in the future!
As I understand, you are receiving this message because of CURLOPT_SSL_VERIFYPEER == FALSE.
Try to remove this string from the code:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
Maybe I wrong, but I think with this option you are receiving "HTTP 501 Not Implemented" error from the Toggl server, which contains exactly the same message, "This method may not be used."

How to make Php rest server send reponse back to client

I am making a simple PHP rest service, I am calling this service with CURL here is the code for this
//client code example
$ch = curl_init($URL);
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
Now on Rest Service I am receiving the request and doing the task. Then I have to send the response back.
//server code example
$xml_post = file_get_contents('php://input');
$xmlparser = new XMLParser;
$parsedata = $xmlparser->parse($xml_post);
$resultobj = new ResultGenerate;
$result = $resultobj->generate($parsedata);
I have no idea how to send the reponse ($result) back.So that $output has the xml string in the end. Please Help
echo $result
is all you need. think about it like this. when using PHP to serve web pages, all you are doing is serving a response to the web browser. You use echo there just like you are using echo here.

Categories