Mario Landa
Mario Landa

Reputation: 17

Get JSON generated by a webhook hosted online

I am making a card payment and then it communicates to the server of the company via webhook, the response is then recorded in RequestBin, which generates a JSON response in their website, how do I extract the information from the website to my PHP code?

The webpage looks like this: my requestb.in online webhook

What I need is to get that raw JSON.

Upvotes: 0

Views: 454

Answers (3)

Mario Landa
Mario Landa

Reputation: 17

I found the solution, first you download HTML dom and then you just change the fields. The reason the for loop goes from 0-19 is because requestb.in saves 20 entries, for the rest just substitute the variables.

    include('../simple_html_dom.php');

    // get DOM from URL or file
    // asegurese de incluir el ?inspect en el URL
    $html = file_get_html('https://requestb.in/YOURURL?inspect');

    for ($x = 0; $x <= 19; $x++) {
        $result = $html->find('pre[class=body prettyprint]', $x)->plaintext;
        if($result){
            $json_a = str_replace('&#34;', '"', $result);   
            $object = json_decode($json_a);
            if(isset($object->type)) echo $object->type . "<br>";
            if(isset($object->transaction->customer_id)) echo $object->transaction->customer_id . "<br>";
        }

    }

Upvotes: 0

Woodrow
Woodrow

Reputation: 2832

You could try using CURL to retrieve the JSON object. Are you using CURL to send the payment payload out to the processor, etc? Below is an example (Obviously you would need to fill in the appropriate PHP variables where applicable).

$reqbody = json_encode($_REQUEST);
$serviceURL = "http://www.url.com/payment_processor";

$curl = curl_init($serviceURL);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);    
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $reqbody);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_VERBOSE, true);    

$headers = array(
    'Content-type: application/json',
    "Authorization: ".$hmac_enc,
    "apikey: ".$apikey,
    "token: ".$token,
    "timestamp: ".$timestamp,
    "nonce: ".$nonce,
);    

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 201 ) {
    die("Error: call to URL $serviceURL failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}

curl_close($curl);

$response = json_decode($json_response, true);

echo "<hr/><br/><strong>PROCESSOR RESPONSE:</strong><br/>";
echo "<pre>";
print_r($response);
echo "</pre>";

Upvotes: 1

Rayann Nayran
Rayann Nayran

Reputation: 1135

You could get the json from requestbin and resend it to your localhost using a request client like Postman.

if (!empty($_POST)) {
    $data = json_decode($_POST);
} 

Upvotes: 0

Related Questions