0

I'm trying to use an online service that requires to read the body of post requests at a callback url (webhook).

However I'm not familiar with http requests and unable to read the received data.

I'm using file_get_contents('php://input') to retrieve the body and store the result in a text file :

file_put_contents("log/test.txt", file_get_contents('php://input'));

I received data look like this in notepad++ (weird characters) :
image

Any idea on what's happening here ?

EDIT : Full code

<head>

    <meta charset="UTF-8">

</head>

<body>

    <?php

        require_once dirname(__FILE__).'/db_functions.php';

        file_put_contents("log/test.txt", file_get_contents('php://input'));

    ?>

</body>

</html>
Bruno
  • 63
  • 6

2 Answers2

2

You are posting binary data.
You are posting an image/wavfile/document.
You can convert it back to what it was and save it to a directory.

$target_dir = "files/";
$target_file = $target_dir . basename($_FILES["file"]["name"]); // file = your input name
$target_file = preg_replace('/\s+/', '_', $target_file);
$uploadOk = 1;
$FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if file already exists
if (file_exists($target_file)) {
    $uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 5000000) {
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        echo "Your file ". preg_replace('/\s+/', '_', basename( $_FILES["file"]["name"])). " is uploaded.";
    } else {
        echo "Sorry, an error occured";
    }
}
Webdeveloper_Jelle
  • 2,868
  • 4
  • 29
  • 55
0

It appears that data were gzipped...

So the simple solution is to use GZDECODE :

file_put_contents("log/test.txt", gzdecode(file_get_contents('php://input')));
Bruno
  • 63
  • 6