How to handle POST data sent from Blockcypher WebHook API

3

I've been trying to get this to work for the past 3 days and I've all but given up on it. Basically, I've used requestb.in to monitor if any POST requests are going through; requestb.in always displays a response. However, I can't replicate this on my own website.

I simply can't find a way to read the body of the response that's getting called back to my server.

For reference, I'm using this sample provided by BlockCypher:

 require __DIR__ . '/../bootstrap.php';

$webHook = new \BlockCypher\Api\WebHook();
$webHook->setUrl("http://mysite.com/dev/data/index.php");
$webHook->setEvent('unconfirmed-tx');

/// For Sample Purposes Only.
$request = clone $webHook;

$webHookClient = new \BlockCypher\Client\WebHookClient($apiContexts['BTC.main']);

/// Create WebHook
try {
    $output = $webHookClient->create($webHook);
} catch (Exception $ex) {
    ResultPrinter::printError("Created WebHook", "WebHook", null, $request, $ex);
    exit(1);
}

ResultPrinter::printResult("Created WebHook", "WebHook", $output->getId(), $request, $output);

return $output;

I had tested the callback url using requestb.in:

http://requestb.in/zphhv1zp?inspect

I've been going crazy testing everything I could find online, but nothing really helped me out. What should I be using to retrieve the POST data?

PS: this is the code that I've been testing most recently

<html lang="en">
<head>
</head>
<body>
    <?php
        $url = "http://requestb.in/zphhv1zp";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        echo $data;

        // $inputJSON = file_get_contents('');
        // $input = json_decode( $inputJSON, TRUE );
        // echo $input['hash'];
    ?>
</body>
</html>

robbythebobby

Posted 2015-08-13T15:41:04.100

Reputation: 33

2While you're showing the code you're using to create the webhook, you're not showing how you're reading the data getting posted by BlockCypher. In PHP $_POST is only valid for forms, while the data BlockCypher POSTs is a JSON document. To access the body of a regular POST (non-form) in PHP use file_get_contents('php://input').Matthieu 2015-08-14T00:14:50.943

@Matthieu thank you for your reply. I've seen that approach and I've tried to implement it to no avail. For some reason php://input didn't return anything, which shouldn't be the case since I know for a fact that the webhook is POSTing data to my callback url.robbythebobby 2015-08-14T13:37:28.987

Answers

1

Full disclosure: I'm BlockCypher's Developer Advocate.

I'm not a PHP expert, but I know a number of our customers have had issues reading return data from PHP. This thread on Stack Overflow might help:

https://stackoverflow.com/questions/8945879/how-to-get-body-of-a-post-in-php

Josh Cincinnati

Posted 2015-08-13T15:41:04.100

Reputation: 645