Convert php curl to GAE urlfetch for iTunes InApp verifyReceipt - php

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}

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

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.

Related

POST Request in PHP is not returning anything

I am trying to use Thycotic PAM API. According to their documentation, The following is a sample HTTP POST request. The placeholders shown need to be replaced with actual values.
POST /SecretServer/webservices/SSWebservice.asmx/GetUser HTTP/1.1
Host: 192.168.3.242
Content-Type: application/x-www-form-urlencoded
Content-Length: length
token=string&userId=string
I can get token string and user ID from the app. With this data, following is the PHP code I am trying
$url = 'https://192.168.3.242/SecretServer/webservices/SSWebservice.asmx/GetUser';
$data = array(
'token' => 'token_string',
'userId' => 8
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = json_decode(file_get_contents($url, false, $context));
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
I also tried this way:
function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = 'https://192.168.3.242/SecretServer/webservices/SSWebservice.asmx/GetUser?token=token_string&userId=8 HTTP/1.1';
$json = json_decode(curl_get_contents($url));
var_dump($json);
Both of them are returning nothing. Any suggestion is much appreciated.
curl_setopt($ch ,CURLOPT_POST, 1);
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch ,CURLOPT_POSTFIELDS, "token=string&userId=string");
you must use this parameters

Invisible recaptcha siteverify - error codes

I'm trying to verify my $_POST['g-recaptcha-response'] on https://www.google.com/recaptcha/api/siteverify but i keep getting the following result:
"success": false,
"error-codes": [
"missing-input-response",
"missing-input-secret"
]
My code:
if($has_errors == false) {
$result = file_get_contents( 'https://www.google.com/recaptcha/api/siteverify', false, stream_context_create( array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query( array(
'response' => $_POST['g-recaptcha-response'],
'secret' => variable_get('google_recaptcha_secret', '')
) ),
),
) ) );
var_dump($result);
$result = json_decode($result);
if($result->success == false) {
form_set_error('name', t('Submission blocked by Google Invisible Captcha.'));
}
}
I checked my variable google_recaptcha_secret, it is correct.
I've never seen file_get_contents used to post data like that, I'm not saying it's not possible but I would recommend trying with cURL:
if ($has_errors == false) {
$data = [
'response' => $_POST['g-recaptcha-response'],
'secret' => variable_get('google_recaptcha_secret', ''),
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
$error = !$result ? curl_error($curl) : null;
curl_close($curl);
var_dump($result);
$result = json_decode($result);
if ($error || $result->success == false) {
form_set_error('name', t('Submission blocked by Google Invisible Captcha.'));
}
}

Opposite method of file_get_content

You know how in PHP there's a method called file_get_content that gets the content of the page for the provided url? Is there an opposite method for it? Like, for example, file_post_content, where you can post data to external websites? Just asking for educational purposes.
You can use without cURL but file_get_contents PHP this example:
$url = 'URL';
$data = array('field1' => 'value', 'field2' => 'value');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
See the PHP website: http://php.net/manual/en/function.file-get-contents.php#102575
Could write one:
<?php
function file_post_content($url, $data = array()){
// Collect URL. Optional Array of DATA ['name' => 'value']
// Return response from server or FALSE
if(empty($url)){
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
if(count($data)){
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$svr_out = curl_exec ($ch);
curl_close ($ch);
return $svr_out;
}
?>

Send notification to specific device using Pushwoosh and Php

Has any one had any success in using Pushwoosh remote api to send custom notifications to a specific device? I have went over their documentation to set this up, but the notification keeps going out to all devices. What am I doing wrong here? Thanks in advance.
<?php
define('PW_AUTH', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('PW_APPLICATION', 'XXXXXX-XXXXXX');
define('PW_DEBUG', true);
function pwCall($method, $data = array()) {
$url = 'https://cp.pushwoosh.com/json/1.3/' . $method;
$request = json_encode(['request' => $data]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (defined('PW_DEBUG') && PW_DEBUG) {
print "[PW] request: $request\n";
print "[PW] response: $response\n";
print "[PW] info: " . print_r($info, true);
}
}
pwCall('createMessage', array(
'application' => PW_APPLICATION,
'auth' => PW_AUTH,
'notifications' => array(
array(
'send_date' => 'now',
'content' => 'Send this content to user',
)
),
'devices' => array('2lksdflkje96a4389f796173fakeae938device95ajkdh8709843') //Optional. Not more than 1000 tokens in an array. If set, message will only be delivered to the devices in the list. Ignored if the applications group is used
)
);
?>
You must specify devices list into notifications array. Please see right request below (Also Pushwoosh API documentation available here https://www.pushwoosh.com/programming-push-notification/pushwoosh-push-notification-remote-api/#PushserviceAPI-Method-messages-create)
<?php
define('PW_AUTH', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('PW_APPLICATION', 'XXXXXX-XXXXXX');
define('PW_DEBUG', true);
function pwCall($method, $data = array()) {
$url = 'https://cp.pushwoosh.com/json/1.3/' . $method;
$request = json_encode(['request' => $data]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (defined('PW_DEBUG') && PW_DEBUG) {
print "[PW] request: $request\n";
print "[PW] response: $response\n";
print "[PW] info: " . print_r($info, true);
}
}
pwCall('createMessage', array(
'application' => PW_APPLICATION,
'auth' => PW_AUTH,
'notifications' => array(
array(
'send_date' => 'now',
'content' => 'Send this content to user',
'devices' => array('2lksdflkje96a4389f796173fakeae938device95ajkdh8709843') //Optional. Not more than 1000 tokens in an array. If set, message will only be delivered to the devices in the list. Ignored if the applications group is used
)
),
)
);
?>

Why does stream_context_create successfully return data but not my Curl?

When I first started, I thought Curl would be an excellent way of retrieving a chunk of data in the format json. It didn't work. I tried doing some Ajax request instead, but that didn't work either.
Now, this is my Curl request:
$ch = curl_init("url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept' => 'application/json',
'Auth' => 'code',
));
$data = curl_exec($ch);
curl_close($ch);
print_r($data);
... The CURL requests RETURNS a EMPTY STRING. No errors...
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: application/json\r\n" . "Auth: code",
)
);
$context = stream_context_create($opts);
$url = "";
$fp = fopen($url, 'r', false, $context);
$r = #stream_get_contents($fp);
fclose($fp);
print_r($r);
Provides a nice array with json data. Why? Isn't this literally supposed to do the same thing?
Because CURLOPT_HTTPHEADER doesn't take associated arrays. You need to add the complete header.
$ch = curl_init("url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Auth: code',
));
$data = curl_exec($ch);
curl_close($ch);
print_r($data);

Categories