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.
Related
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;
}
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.
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.
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.
you can find a similar question here PHP Post Request inside a POST Request but this is not working in my context.
I have a form (reservation form for a tour website) and when the form is submitted, the values are processed in a script like validation and calculation of values and sending email.
after processing the variables, i want to send it to a page for payment and this page will post payment details to paypal.
My question is after the reservation form is submitted, after processing values retrieved from reservation from, how can i redirect the page in such a way that the variables will be passed as post variables. (I am not looking from response from the other form, i want to redirect to the other form).
To create a POST request, open a up a TCP connection to the host using fsockopen(), then use fwrite() on the handler returned from fsockopen() with the same values you used in the header functions in the OP. Alternatively, you can use cURL.
<?php
if(isset($_POST['Name'])) $Name = $_POST['Name'];
if(isset($_POST['Email'])) $Email = $_POST['Email'];
if(isset($_POST['Message'])) $Message= htmlentities($_POST['Message']);
$Curl_Session = curl_init('http://www.yoururl.com/script.php');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "Name=$Name&Email=$Email&Message=$Message");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);
?>
Where $Message would be your variables.
EDIT: i answered this long ago maybe i did forget to reference from php.net but here is the link per comment http://www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html as Reference
You can't. HTTP makes no provisions for redirecting with anything in the request body (which is where POST data goes). You can only redirect with a GET request. So the typical way to do this is to take the user to a second page that has a button to "Continue to PayPal", or something of that nature. That button POSTs all the data to PayPal as normal.
For what it's worth, if this is for the PayPal "Buy Now" button, they actually (even if not documented) allow sending all those form variables in the GET request, via the URL. We do this in one of our applications where we "track" the start of the payment process and then redirect to a PayPal URL containing all the form fields as a query string, then "complete" the transaction as the user returns.
Well, I suppose that if you are talking about keeping data in your own website between pages you need to use PHP's session functions
To start the session just do session_start();
and to add session vars just use the superglobal array $_SESSION['myvar'] = $value;
you can then read them through the same means print_r($_SESSION[]);
However if you are talking about sensing data with the paypal API I highly recommend looking at their developer API manual.
Hope that helps,
RayQuang