我正在进行卡支付,然后通过webhook与公司服务器通信,然后将响应记录在RequestBin中,在其网站中生成JSON响应,如何从网站提取信息到我的网站PHP代码?
网页如下所示: my requestb.in online webhook
我需要的是获得原始JSON。
答案 0 :(得分:1)
您可以尝试使用CURL来检索JSON对象。您是否使用CURL将付款有效负载发送到处理器等?下面是一个例子(显然你需要填写适当的PHP变量)。
$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>";
答案 1 :(得分:0)
您可以从requestbin获取json并使用Postman等请求客户端将其重新发送到您的localhost。
if (!empty($_POST)) {
$data = json_decode($_POST);
}
答案 2 :(得分:0)
我找到了解决方案,首先下载HTML dom,然后只需更改字段即可。 for循环从0到19的原因是因为requestb.in保存了20个条目,其余的只是替换变量。
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('"', '"', $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>";
}
}