iPhone Push Notification - Error response problem - php

I've got a problem when checking for a response error after sending a Push Notification. This is my setup:
From my PHP server, I'm sending Push Notifications. These notifications are send in the enhanced format, so I can get an error response from the Apple server. For example: Error #7 "Invalid payload size".
The way I check for errors is reading the socket response:
const ERROR_RESPONSE_SIZE = 6;
$errorResponse = #fread($this->_apnsSocket, self::ERROR_RESPONSE_SIZE);
This works fine when there is an actual error. By my problem is: when there's no error, the "fread" call doesn't return anything and keeps loading forever.
Can anyone help me with this? Thanks for your help!

You need to set stream_set_blocking($this->_apnsSocket, 0); to 0 which is non-blocking mode, because on success Apple doesn't send back anything, but the fread is waiting for data in blocking mode.

Related

Invalid Drm Argument Exception Validating App Store Receipt

let url = Bundle.main.appStoreReceiptURL!
let receipt = NSData(contentsOf: url)?.base64EncodedString(options: [])
The receipt string works well when I send it to Apple servers, but when my server sends it i get this error:
"status":21002, "exception":"com.apple.its.drm.InvalidDrmArgumentException"
I have no clue why, especially since it was working yesterday.
Turns out the HTTP POST function variables were all being sent to APPLE.
I had thought the receipt=XXX&key=1234 were separated in the PHP Server side code.
But the '&' wasn't there so the receipt sent to Apple was XXXkey=1234
so com.apple.its.drm.InvalidDrmArgumentException means that the receipt data has an error/ extra data.

php webhook not responding to Stripe test event

I've setup a basic webhook php page as modeled on the stripe documentation and listed below. When I send a test event from the Stripe webhooks dashboard, stripe responds "Test webhook sent successfully" with a blankk reponse. However, the output log file is not written to, no email is sent and there is nothing logged to the http server error log or the php error log. My php version is 5.3.3. What am I doing wrong?
<?php
error_reporting(15);
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("secret_test_key");
$handle = fopen("webhook.log","a");
// Retrieve the request's body and parse it as JSON
$input = file_get_contents("php://input");
$event_json = json_decode($input);
// Do something with $event_json
if (fwrite($handle, $event_json) === FALSE) {
mail("mike#example.com","Cannot write to webhook.log","");
echo "Cannot write to webhook.log";
exit;
}
mail('mike#example.com','Webhook Event',$event_json);
header(':', true, 200);
//http_response_code(200); // PHP 5.4 or greater
?>
You have a few potential problems. As a quick rule of thumb, the best way to debug this is to start by triggering the event yourself, which you can do simply by loading up your webhook url in a browser yourself. Then you can test it directly and make sure it is doing what you expect it to be doing. There are obviously two possibilities:
Stripe is not triggering your webhook handler for some reason
Your handler is not properly logging itself
The latter first: it could be that Stripe is triggering your handler but it isn't logging that fact successfully. This would mean that both your email logging and file logging are failing. That is actually quite possible. Email logging with the mail function is actually very unreliable, unless you know for a fact that it works. Mail sent with the mail function is dropped silently by most modern email systems (gmail, etc) unless you have your DNS records properly configured, which most people don't. So unless you know for sure that your mail attempt is working properly, it probably isn't. If you also happen to have a permission issue in your attempt to write to a log file (which is not uncommon for a newly setup server), your logs could simply be failing. The easiest way to check that is to load up the webhook URL in a browser yourself. That way you know it is being triggered, and can know for sure if the issue is improper logging or Stripe not calling your webhook.
If you determine for sure that stripe isn't calling your webhook, the most likely culprit would be an invalid HTTPS certificate. Is your webhook connected via HTTPS (it should be)? If so, is it a valid certificate? You can tell your browser to ignore an invalid certificate when you browse your own site, but stripe will simply refuse to send the request if it encounters an invalid certificate.
If none of the above fixes it then it will be time for more digging, but I would start with those: they are probably the most likely problems.
The solution is that $event_json is an object and the fwrite failed because it expects a string not an object. By converting to an array and then serializing I was able to both write to the log and send the email.
$event_json = (array)json_decode($input);
$event = serialize($event_json);

php http post response for web hook

I'm trying to create a web hook notification. The documentation of the service i want to use requires that i specify a URL where POST requests can be performed. This URL will receive the following object, in json format, and must respond with a Status Code between 200-299.
{
"type": "ping"
}
I don't know how to proceed making my server on localhost respond with a 200 status code. http_response_code(200) works well on live server but nothing seem to be happening on localhost.
Is there any way i can make it work with localhost?
I've included the link to the documentation here (i hope it's not against the rule).
I am thinking that you wouldn't have to send them the response. The webhook would know about the response. If it reached your URL successfully, it would be a 200 OK right off the bat. If the API is requesting a response back then I imagine that you would have to call it back somehow. Is this a well-known API? Any documentation?
The response code is in the response header, not in the content.
PHP defaults to a response code of 200, so if you don't mess with it at all, you should be good.
If you want to set a different response code (202 for example), just call:
http_response_code(202);
Or set the full header yourself:
header('HTTP/1.1 202 Accepted');
Proper way to explicitly set 200 (or any other) status code with http_response_code function is just as following (don't echo or json_encode it):
http_response_code(200);
It should force webserver to use 200 status code in it's response. However, webserver could possibly ignore it. To check what response code your webserver sends, use telnet or any REST tool like Postman

PHP web service, send response before end of script execution

I have a web service written in PHP to which an iPhone app connects to. When the app calls the service, a series of notification messages are sent to Apple's APNs server so it can then send Push Notifications to other users of the app.
This process can be time consuming in some cases and my app has to wait a long time before getting a response. The response is totally independent of the result of the notification messages being sent to the APNs server.
Therefore, I would like the web service to send the response back to the app regardless of whether the messages to APNs have been sent.
I tried using pcntl_fork to solve the problem:
<?php
...
$pid = pcntl_fork();
if($pid == -1)
{
// Could not fork (send response anyway)
echo "response";
}
else if($pid)
{
// Parent process - send response to app
echo "response";
}
else
{
// Child process - send messages to APNs then die
sendMessageAPNs($token_array);
die();
}
?> // end of script
Unfortunately, the parent process seems to wait for the child process to end before sending the response even though I do not use pcntl_wait in the parent process. Am I doing something wrong or is this normal behaviour? If this is normal then is there another way I can solve this problem?
Thank you!
If you're hosting the PHP process in Apache then you really shouldn't use this: see this for the section that says *Process Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web server environment. *.
You should probably set up a separate daemon in your preferred language of choice and hand the APNS communication tasks off to that. If you really really really must try using ob_flush().
I think you can send the response back before doing the "long" process. Take a look at the flush() function of PHP it'll maybe help

Swift Mailer vague response code

Have you encountered this error?
"Expected response code(s) [250] but got response []"
There's no response code at all.
I just followed the basic usage of the module. Something like this:
$email = new Email("Subject");
$email->to('recipient#mail.com');
$email->message('Message');
$email->send('no-reply#site.com');
Not sure on the SMTP configuration though.
Any solution? Thanks.
The 250 code represents an acknowledgement of the last client request. If the server is not responding then either the swiftMailer class is very bugy (improbable) or there's something very weird in the server its connecting to.
Have you tried connecting to the same server using a conventional MUA?
C.
I am getting the same issue however slightly different syntax, but still comes to the same thing
Expected response code 250 but got
code "", with message ""
Its only started happening since I have pointed my scripts at a different mailserver. The strange thing is, the mails actually seem to be getting through. Will post back with my findings

Categories