Dropbox API PHP - notification request is empty - php

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.

Related

PayPal Webhook sends no POST data

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.

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

Receiving JSON in php, and using data

I have been trying to set up a Shopify webhook (documentation here) through the Shopify admin section. So far I have not been able to grab any of the data being sent in a test webhook using multiple methods, but I know it is sending because I created a Requestbin and have been seeing data come through.
I found this chunk of code here on Stackoverflow.
header('Access-Control-Allow-Origin: *');
$postdata = file_get_contents("php://input");
$file = "log.txt";
$current = file_get_contents($file);
$current .= $postdata;
file_put_contents($file, $current);
echo $current;
So far it is the only code I have been able to use to actually see any JSON. But Im not wanting to write the JSON to the "log.txt" file every time the webhook is fired. But once I try to remove or adjust the code in anyway I no longer see any JSON. It seems that I have to write $postdata to a file, and then retrieve the contents to get an array.
Is is possible to access the JSON without having to first write it to another file?
So to begin, my fundamental thought process was flawed on how webhooks worked.
header('Access-Control-Allow-Origin: *');
$postdata = file_get_contents("php://input");
$file = "log.txt";
$current = file_get_contents($file);
$current .= $postdata;
file_put_contents($file, $current);
echo $current;
The reason this code needed to write the contents of file_get_contents("php://input") to the text file was because the POST request sent by the Shopify webhook, only hits the page briefly. As soon as you refresh the page, you lose all the POST data...seems obvious I know. That said I needed a way to determine if the JSON Shopify is sending out is working all fine on my server. So I created this...
$json = json_decode(file_get_contents("php://input"));
file_put_contents('./realRequests.txt', $json->id . "\n", FILE_APPEND);
What this does is capture the ID of an order everytime Shopify fires the webhook, and wrties it to a new line in a requests file. That way you know you are able to recive working code from shopify.

Get and Send file with PHP and RESTful API

One of my partners has an API service which should send an HTTP POST request whenever a new file is published. This requires me to have an api file which will get the POST this way:
http://myip:port/api/ReciveFile
and requesting that the JSON format request should be:
{
"FILE ":"filename.zip",
"FILE_ID":"123",
"FILE_DESC":"PRUPOUS_FILE",
"EXTRAVAR":"",
"EXTRAVAR2":"",
"USERID":" xxxxxxxxxxxx",
"PASSWORD":"yyyyyyyyyyy"
}
Meanwhile it should issue a response, in JSON format if it got the file or not
{"RESULT_CODE":0,"RESULT_DESCR":""}
{"RESULT_CODE":1001,"RESULT_DESCR":"Bad request"}
And after, when I am finished elaborating the file, I should send back the modified file same way.
The question is, now basically from what I understand he will send me the variables witch I have to read, download the file, and send a response back.
I am not really sure how to do this any sample code would be welcomed!
I'm not sure exactly what the question is, but if you mean creating a success response in JSON for if an action occurred while adding data to it which is what can be understood from the question, just create an array with the values you wish to send back to the provider and do json_encode on the array which should create json and just print it back as a response.
About receiving the information; all you have to do is use the integrated curl functions or use a wrapper (Guzzle, etc) to output the raw JSON or json_decode data into a variable and do whatever you please with it.
From what I read in the question, you wish to modify the contents of it. This can be achieved by just decoding the json and changing the variables in the array, then printing the modified JSON back as a response.
Example (this uses GuzzleHTTP as an example):
$res = $client->request('GET', 'url');
$json = $res->getBody();
$array = json_decode($json, 1); // decode the json
// Start modifying the values or adding
$array['value_to_modify'] = $whatever;
$filename = $array['filename']; // get filename
// make a new request
$res = $client->request('GET', 'url/'.$filename);
// get the body of specified filename
$body = $res->getBody();
// Return the array.
echo json_encode($body);
References:
http://docs.guzzlephp.org/en/latest/

Slim PUT request empty body

I am using slim php framework for developing REST API. I am successful in implementing POST and GET requests. I am using ContentTypes middleware as well to parse the JSON body in POST and PUT requests however my PUT request always gives empty string on the server. POST just works fine and I can get the parsed JSON as PHP associative array but cant get it in PUT request. I am using application/json in headers and I dont want to use application/x-www-form-urlencoded method.
$app->map('/example/:id', function ($id) use($app, $log) {
//$body = $app->request()->getBody();
//using the above in other POST calls & it works but does not in this case
$body = json_decode($app->request()->getBody()); //tried this. no success
var_dump($body);
} )->via ( 'PUT', 'PATCH' );
I am calling it via CURL like this
$headers = array(
'Content-Type'=>'application/json;charset=utf-8',
);
$id = 123;
$body = array("name"=>"myfirstname","email"=>"myemail");
$json_str = json_encode($body);
$response = Requests::put($base_url.'/api/v1/example/'.$id,$headers,$json_str);
When I try to return the same JSON from the API it returns empty array. I tried POSTMAN on chrome and above code but does not work. What is the issue.
Update: I have verified the same code works on localhost but does not work on remote dev server. What can be the reason? Do I need to alter any settings on server?
Slim reads php://input to get the contents of the request body, so whatever the problem is it has to do with the particulars of that stream.
Do you have some other code that tries to read php://input? If so, note that this is only possible starting from PHP 5.6 (which your local machine may have when your server does not).
Try using getInstance().
$body = json_decode($app->getInstance()->request()->getBody());

Categories