0

I've read many 'possible duplicates' of this question and have used the code from one of them in this post but I cannot seem to see exactly what this question is asking.

As part of a much bigger project unrelated to PHP or websites I need to obtain the body of a POST request to my shared server. Following the ideas from this SO post I have made a php file in the www folder on the server containing the following

webhook.php

<?php 
error_log("webhook running");
file_put_contents("post.log", print_r($_POST, true));
?>

I then used a third party company to POST a test message to that script on my server, which was received OK, with a 200 response.

The body of that post contained

Body
{
  "events": [
    {
      "id": "EVTESTDCQAHTYRDYM72",
      "created_at": "2021-01-04T12:17:41.536Z",
      "resource_type": "payments",
      "action": "paid_out",
      "links": {
        "payment": "index_ID_1234567"
      },
      "details": {
        "origin": "mycompanys",
        "cause": "payment_paid_out",
        "description": "The payment has been paid out by mycompany."
      },
      "metadata": {}
    }
  ]
}

but the log file on my server contains only

Array
(
)

I'm not a PHP programmer (just do the minimum I need to in order to get the job done) For debugging and development purposes, how should I alter my webhook.php script so that I can see all the data that was sent?

If I can do that then I can probably work out how do do the proper processing on each element, which won't always have the structure above.

Cid
  • 14,968
  • 4
  • 30
  • 45
user2834566
  • 775
  • 9
  • 22
  • Have you checked your error logs. That code really should work, so maybe there is a useful error message in the log – RiggsFolly Jan 04 '21 at 12:48
  • Add [error reporting](http://stackoverflow.com/questions/845021/) to the top of your file(s) _while testing_ right after your opening PHP tag for example. Even if you are developing on a server configured as LIVE you will now see any errors. ` – RiggsFolly Jan 04 '21 at 12:51
  • 2
    *"I then used a third party company to POST a test message"* you may not have enabled CORS – Cid Jan 04 '21 at 12:51
  • 1
    Does this answer your question? [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) – CBroe Jan 04 '21 at 12:59
  • @RiggsFollym Yes, the error log contains `[04-Jan-2021 12:21:53 Europe/London] webhook running [04-Jan-2021 12:21:58 Europe/London] webhook running [04-Jan-2021 12:24:14 Europe/London] webhook running [04-Jan-2021 12:28:11 Europe/London] webhook running` – user2834566 Jan 04 '21 at 15:22
  • @ Cid 2 What is CORS? and how would I enable it - or even know that I have to? – user2834566 Jan 04 '21 at 15:23
  • @CBroe No it didn't I'm afraid. All the answers use `file_get_contents('php://input')` which I'm assuming is reading from a file but I don't have a file, just a POST coming in so don't know how to translate this line. Anyway, using this code - with event instead of operation, still just gave me array(). Also I don't know where the print_r() and echo are sending their output. I'm not running the php from a browser. As far as I understand it gets run by the POST request somehow. Seems to be anyway.. – user2834566 Jan 04 '21 at 15:35
  • I've read up on what php://input is and have tried `$data = json_decode(file_get_contents('php://input'), true); error_log($data); ` (which gave an error and error_log($data[0] which showed nothing in the error log); ` If this is such a bad question to ask that it is worth voting it down without explanation, maybe someone who knows a lot more about php than me could show me how they would get the whole of the body sent as plain text to the error log. (I didn't ask to decode it) It. It must be a very common requirement when testing code that is sent data via a POST – user2834566 Jan 04 '21 at 15:50
  • I assume you got `Array to string conversion` in your latest attempt. Have you tried logging `print_r($data)`? Or perhaps `json_encode($data)` (anything that would make it a string). – El_Vanja Jan 04 '21 at 15:58
  • @ El_Vanja "I assume you got Array to string conversion" Sorry, I don't understand what this means. " Have you tried logging print_r($data)? Yes, I tried `` The error log showed `18:33:23 Europe/London] webhook running [04-Jan-2021 18:33:23 Europe/London] 1' - whatever that means... Why is a question from a php newbie voted down without explanation yet those that have probably done this already are unwilling to show me how they did it. – user2834566 Jan 04 '21 at 18:44
  • 1
    PHP only populates $_POST, when the request Content-Type was either `application/x-www-form-urlencoded` or `multipart/form-data`. If you get send anything else, then you have to use `php://input` to read the raw POST body, and parse it yourself. The example JSON you have shown appears to be valid - so if `json_decode` does not give you the expected result here, then your input data was probably not what you expected it to be in the first place. – CBroe Jan 05 '21 at 07:38
  • 1
    A basic debugging mistake here is that you are only looking at the final result of multiple “compound” operations. Log what `file_get_contents('php://input')` returned first of all, to see if that actually is what you expected it to be. If it wasn’t to begin with, then looking only at what `json_decode` returned, isn’t that helpful. – CBroe Jan 05 '21 at 07:40
  • @CBroe Ah, Thanks, that's exactly what I was asking for. A way to simply shows me the entire POST body. Now I know what gets sent I can probably work out how to get each element and do the correct processing. So in spite of the downvote by some unhelpful person, and users asking if I've tried things I've never heard of, the answer to my original question "how should I alter my webhook.php script so that I can see all the data that was sent?" is yours. Simply use `error_log(file_get_contents('php://input') )` If you want to put that as an answer I will give you all the points. – user2834566 Jan 05 '21 at 11:27

1 Answers1

1

PHP only populates $_POST, when the request Content-Type was either application/x-www-form-urlencoded or multipart/form-data. If you get send anything else, then you have to use php://input to read the raw POST body, and parse it yourself.

The example JSON you have shown appears to be valid - so if json_decode does not give you the expected result here, then your input data was probably not what you expected it to be in the first place.

A basic debugging mistake here is that you are only looking at the final result of multiple “compound” operations. Log what file_get_contents('php://input') returned first of all, to see if that actually is what you expected it to be. If it wasn’t to begin with, then looking only at what json_decode returned, isn’t that helpful.

CBroe
  • 91,630
  • 14
  • 92
  • 150