PayPal Webhook sends no POST data - php

Configured my PayPal webhook successfully to be notified of all events.
PayPal calls my webhook (a simple script) when events occur... but does not send any POST data with it....
All PHP arrays are empty ($_POST, $_GET, $_REQUEST) except for $_SERVER of course.
What is going on? The webhook simulator says that the events are sent/queued succesfully...
The $_SERVER array contains the suggested HTTP_PAYPAL_... headers and everything.... but the $_POST array is empty.
My webhook is written as follows...
<?php
require ('./ace-includes/ace_log.php');
ace_log(print_r($_POST, true));
ace_log(print_r($_REQUEST, true));
ace_log(print_r($_GET, true));
ace_log(print_r($_SERVER, true));
ace_sendlog("NOTIFY SCRIPT CALLED");
?>

I figured it out....
You cannot use $_POST to get the data....
The data is contained in the HTTP file sent to the script.
In this case you MUST use
file_get_contents('php://input');
to get the data.
So for PayPal PHP webhooks you would do this....
$json = file_get_contents('php://input');
$data = json_decode($json);
This works and now I am getting all the data.
This really should be documented somewhere.... anywhere... but its not... and not obvious to an beginning PHP programmer.

Related

API endpoint: Receive post vars with $_POST or file_get_contents('php://input')?

I'm working on an API endpoint that receives notifications when payments have gone through. The gateway I'm currently implementing is a chilean bank transfer gateway called Khipu. When they send the notification, I have to receive the POST variables using $_POST as opposed to json_decode(file_get_contents('php://input')). It seems Khipu handles POSTs in a way that's a bit unconventional, no? What is the difference between how Khipu does it, and all others? How is Khipu sending these POST vars? Why do I have to receive the variables from them using $_POST? To avoid any problem in retrieving variables, I'm using the following code, which seems to work fine:
if (empty($_POST)) { //For strange posts used by Khipu
$postVars = json_decode(file_get_contents('php://input'));
} else {
$postVars = $_POST;
}

PayPal Webhooks in Sandbox not working (Null Data)

I'm having issues with setting up webhooks with PayPal.
Actions Taken:
I have set up a hook to alert me of completed payments.
I do notice that PayPal does hit the requested url. However when I do a complete log of $_POST, $_Get and $_COOKIES there is nothing sent.
My understanding as that the server is meant to send data in the POST form.
Any help would be greatly appreciated.
Thanks.
Update: Solution to this FOUND!!!
$request_body = file_get_contents('php://input');
$request_body = json_decode($request_body);
$request_body = print_r($request_body, true);
I found that PayPal posts the JSON string a different way other then via POST. The above code is how I was able to retrieve the required data.
Thanks again guys for the input.

How do I get data from Webhook to store in a database?

I am working with a Webhook based of of SendGrid's API v3. The Webhook is completely set up with an endpoint that SendGrid posts to, but I need to be able to receive that parsed data and store it in a database using PHP, but do not know how to do that.
I have worked with APIs before where I initiate the retrieval or POST of data, but when the API server is the one POSTing, how do I catch the data being parsed through the Webhook endpoint? I have some simple thus far, but am really unclear of how to tackle this:
<?php
$json = file_get_contents('https://example.com/webhook.php');
$json_decoded = json_decode($json, true);
$var_dump(json_decoded);
?>
I have tried sending multiple emails to the host, but every time I make this call I return NULL.
Try using the code from this example. This will give you the raw HTTP request body bytes to decode.
<?php
$data = file_get_contents("php://input");
$events = json_decode($data, true);
foreach ($events as $event) {
// Here, you now have each event and can process them how you like
process_event($event);
}
?>
More info on php://input

Dropbox API PHP - notification request is empty

I have what should be a very simple issue to solve, but I can't figure out what is going wrong.
Just started a project to use the new Dropbox API v2 to receive notifications for file/folder changes. Following the steps provided in the documentation provided, but I run into an issue right off the bat.
I've verified the webhook, and I do receive a POST request from Dropbox every time a file is changed, but the POST request just contains an empty array. The code is simple, as I have just begun the project:
// USED for initial verification
/*
$challenge = $_GET['challenge'];
echo $challenge;
*/
$postData = $_POST;
$post_dump = print_r($postData, TRUE);
$fpost = fopen('postTester.txt', 'w');
fwrite($fpost, $post_dump);
fclose($fpost);
$postData is an empty array with sizeOf() 0.
Any ideas?
Here is the updated code with the solution. Very simple fix.
$postData = file_get_contents("php://input");
$post_dump = print_r($postData, TRUE);
$fpost = fopen('postTester.txt', 'w');
fwrite($fpost, $post_dump);
fclose($fpost);
I believe this is because $_POST is only for application/x-www-form-urlencoded or multipart/form-data Content-Types. The payload delivered by Dropbox webhooks is application/json.
It looks like you'll instead want to use $HTTP_RAW_POST_DATA or php://input, depending on your version of PHP.
You can get the raw payload and then json_decode it to get the structured information.

problems with php POST - POST body is empty in login request

I am currently working on an APP that sends a post request to a server to login. The backend developer of my team is a bit slow so I thought why wouldn't I just set up some mock urls for testing the app.
I got a php file for the login which should return me a session when I enter correct data. Unfortunately I am stuck trying to get the JSON i post into some variables.
In fact, i don't see any data i receive. I've tried to use vardump on $_POST, $HTTP_RAW_POST_DATA as wel as file_get_contents("php://input"). I tried to decode the latter two with json_decode.
Some give me String(0) "" others give me NULL. In some cases i just got nothing.
Using retrofit i send a request to /login with a body of
{
"password":"password",
"username":"Marcel"
}
However when what i get as return is nothing. The my php code is only getting the and returning a json object with a session token
if(!empty($_POST["username"]) && !empty($_POST["password"])){
$id= array('SESSION_ID' => 'random_session_token_0015');
echo json_encode($id);
}
This code example does not give me any return. Using print_r($_POST); i will get an empty array like array().
$json = file_get_contents('php://input');
$object = json_decode($json);
var_dump($object);
This code example does give me any return, but it is NULL. $HTTP_RAW_POST_DATA had the same result as the latter example.
I'm quite stuck in this problem, anyone there to help?
ps. aks if you need more info
I resolved the same problem by making sure I send the "Content-Type: application/json" header to my Node.js server.
Try
file_get_contents(
'php://input',
false,
stream_context_get_default(),
0,
$_SERVER['CONTENT_LENGTH']
);

Categories