Processing POST data from a third party - php

I have been struggling with the following problem for few days. I am expected to receive POST data from a third party company for certain real time events. I have been in contact with them as to why my script isn't seeing anything. Unfortunately, they are less then helpful and just tell me "it works for everybody else".
I setup a simple PHP script after doing some research on the web but it always shows no post data. This isn't my area of expertise so I may be missing something obvious.
Here is the only documentation they give:
This API will send a real-time http request upon every successful event with all of the conversion data. The data is sent via an http POST method, and the data is JSON formatted.
This is my script which for now is trying to just log it to a file. The file is created and I see the IP address of the request but the output is empty in terms of post data.
ob_start();
echo "Request from :" . $_SERVER[REMOTE_ADDR];
echo "print_r:".print_r($_POST,true);
if(array_key_exists('app_id', $_POST))// me attempting to access a specific key they claim is in the post data
{
echo "app id = " . $_POST['app_id'];
}
//I also tried both of these and neither output anything
//foreach ($_POST as $key => $value) //idea 1
foreach($_POST as $item) //idea 2
{
//echo "key=".$key." value=".$value; //idea 1 log
echo "next=";//idea 2 log
echo $item;
}
$contents = ob_get_flush();
file_put_contents("log.txt",$contents,FILE_APPEND);

There's not a lot to go on here -- who knows what the client is actually sending -- but here's a thought:
POST is just an HTTP command. It's traditional for the body of a POST to be a series of key-value pairs from a form, but it is not actually necessary. It's possible that the remote client is issuing a POST request to your server and then just delivering a JSON blob in the request body, which would not be successfully parsed into the $_POST array.
I recommend exploring the answer at How to get body of a POST in php? to see if that helps shed light on this problem.

Related

How do you send back data when you get a webhook?

I have a quick question. I am new to webhooks and the service I am using requires a response. I am doing this in php and here are their instructions:
We require you to verify the ownership of the server you are making WebHook calls to by adding the following object parameters to your JSON response.
{ "details" : { "zippy_token":"xxxxxxxxxxxxxxxx" }}
After adding the JSON response code to your WebHook endpoint, come back here and click the green Add Webhook button. Zippykind will verify your WebHook by making a POST call to your WebHook URL with the handshake within the parameters, afterwhich will add the verified WebHook to your active list of WebHooks. You only need to do this once, after the WebHook has been verified, you can remove the zippy_token parameter from your JSON response.
I see how to get the data but how do I send the info needed (the token) back?
This will be done in PHP.
Thanks
If your site, or what you're developing is located at abc.com, you'd essentially need to create a php script for your webhook callback. abc.com/testwebhook.php.
Inside of the testwebhook.php, you'll output a JSON response with the data formatted as they expect to receive it { "details" : { "zippy_token":"xxxxxxxxxxxxxxxx" }}
In case the service you're interacting with (Zippy) is checking the header output of your response, you may need to set the header via PHP within your testwebhook.php script: header('Content-Type: application/json');
Example - testwebhook.php
`<?php
$output = '{ "details" : { "zippy_token":"xxxxxxxxxxxxxxxx" }}';
header('Content-Type: application/json');
echo $output;
exit;
?>
You'll need to ensure that your json is properly formatted and the endpoint doesn't expect more data returned.
Then the rest is explained ini your initial question. Create the webhook by entering the URL at the Zippy service that gave you those instructions and add the url to the script you setup abc.com/testwebhook.php.
That should be all you need to do.

Receiving external data in a Server Sent Event and PHP

I'm having trouble understanding Server Sent Events, particularly on the server side. All the examples I find are basically just sending the server time, which isn't terribly helpful.
I'm trying to bring data in from an external resource that is sending a POST request to the file, specifically a Twilio webhook whenever a new message is received.
The problem that I'm running into is that the data sent from the webhook isn't accessible from my request to the Server Sent Event, which makes sense when you think about it because they're two different sessions.
I could create create another file for the webhook to access and add the message data to an SQL database each time a new message is received. Then, in the file that is my serves as my EventSource, I could query that data and see if it's changed, but doesn't that seem horrible inefficient to be querying an SQL database every three seconds, even if I am comparing the data using a session variable or something?
Ideally, it would be something like this:
header('Content-Type: text/event-stream; charset=utf-8');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
header("Access-Control-Allow-Origin: *");
$body = $_REQUEST['Body'];
$finalbody = !empty($body) ? $body : 'nothing to see here...';
echo "id: $id" . PHP_EOL;
echo "data: $finalbody" . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
But that obviously doesn't work when you use it as the EventSource because $_REQUEST['Body'] can't be found.
What good is are Server Sent Events if they have to be constantly querying an external source rather than having data sent to them on-demand and then using that data to send the event on it's way?
Am I missing something?
Any insight here would be much appreciated.

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.

How do I echo a payload of JSON data sent via POST method in my browser?

I am trying to visually see the JSON data that gets sent from a webcam site in order to verify that it is being received. I built a php handler and pointed the webhook at it, yet it is not showing up in the browser or in the network traffic. Here is the JSON data:
Body
payload={
"version":"1.0",
"event":"video_recorded",
"data":{
"videoName":"randomstringofstuff",
"audioCodec":"NellyMoser ASAO",
"videoCodec":"H.264",
"type":"FLV",
"orientation":"landscape",
"id":"278264",
"dateTime":"2017-04-04 23:06:29",
"timeZone":"America/Los_Angeles",
"payload":"",
"httpReferer":"http://www.whateversite.is/mytest/"
}}
From the site where I input the URL for my php script it says I can retrieve the data with:
$data = $_POST["payload"];
$retrievedData = json_decode($data, true);
I tried to echo the $retrievedData Variable:
echo $retrievedData;
However, when I do so and try sending another test payload and refreshing the page I don't have anything on the page. I have also tried:
print_r['$_POST'];
which just returns:
Array ()
From the site where I initiate the POST it returns that it went through successful, and according to the help desk the POST won't appear in the network traffic due to it being sent from a server, not their site. What am I doing wrong?

cURL feedback with some value (true/false)

So I was wondering if it is possible got get a value back as feedback after sending a cURL request. I got a project going on where I send XML data to a Press office, where they check if the data is valid and the press process can begin. Now I need some feedback. I know I can get a message back when i do -
echo 'not enough paper'
or echo 'we can start'
on that page (I mean if they do). But I need a value to like true or false, so I can set a status on my side. I will keep googling and just leave this question here, maybe some one got an idea.
If you are sending some data to an external service or URL, only they can decide to put a status message in the response.
I assume you are getting an HTTP 200 code which indicates that the request was successful, but what the other side does with the request is totally up to them. You might have to request that they add some additional parameters to their response.
Depending on what you ask them to do, you would parse their response that you get when you execute the cURL command -
$retValue = curl_exec($request);
// if they return XML data
$retXML = new SimpleXMLElement($retValue);
// if they return JSON data
$retJSON = json_decode($retValue);
You should use what ever method you feel more comfortable with. If you are already dealing with XML data within your code, then just request that they return some XML data with a status message.
Example return data -
XML
<response>
<status value="true" />
<message value="We can start!" />
</response>
JSON
{ 'status':'false','message':'not enough paper!' }

Categories