我是php网络服务的新手,之前我已经问过这个问题,但是无法获得有用的解决方案,所以我试着改写它。我试图听取API的响应并更新我的数据库,就像paypal IPN服务所做的那样。到目前为止,我在浏览器中运行它时脚本运行良好,并且我通过POST回复了json响应,但是API后来发送了另一个响应而没有浏览/客户端的参与,我希望它能够进行通信到我在服务器上的PHP脚本并更新数据库。 API有一个notifyURL字段,它需要它发送这些通知,我已将其设置为发送请求的同一文件。下面是我到目前为止的代码:
if($ch !== false){
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Set the content type to application/json
// curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;odata=verbose',
'Content-length:' . strlen($jsonDataEncoded))
);
}
//Execute the request
$result = curl_exec($ch);
if ($result === false){
curl_close($ch);
echo "Error".curl_errno($ch). "\n";
}else {
curl_close($ch);
$decoded = json_decode($result, true);
// echo '<pre>';
// var_dump($decoded["paymentAmount"]["totalAmountCharged"]);
// die;
$data['unique_id'] = $decoded["clientCorrelator"];
$data['subscriber_number'] = $decoded["endUserId"];
$data['payment_status'] = $decoded["transactionOperationStatus"];
$data['payment_gross'] = $decoded["paymentAmount"]["totalAmountCharged"];
$data['txn_id'] = $decoded["ecocashReference"];
$this->payments->insertTransaction($data);
die;
我需要为脚本添加什么才能使用curl和php实现我的目标。 API返回下面的json响应,我正在尝试监听它并将某些字段更新到数据库中。
{"id":71109,"version":0,"clientCorrelator":"58ada3aec8615","endTime":null,"startTime":1487774641697,"notifyUrl":"http://website/ecocash_send/transaction","referenceCode":"17589","endUserId":"774705932","serverReferenceCode":"2202201716440169792864","transactionOperationStatus":"PENDING SUBSCRIBER VALIDATION","paymentAmount":{"id":71110,"version":0,"charginginformation":{"id":71112,"version":0,"amount":1,"currency":"USD","description":" Bulk Sms Online payment"},"chargeMetaData":{"id":71111,"version":0,"channel":"WEB","purchaseCategoryCode":"Online Payment","onBeHalfOf":"PAMONMEFT","serviceId":null},"totalAmountCharged":null},"ecocashReference":"MP170222.1644.A00059","merchantCode":"0000","merchantPin":"000","merchantNumber":"771999313","notificationFormat":null,"serviceId":null,"originalServerReferenceCode":null}"
答案 0 :(得分:0)
响应PayPal IPN的脚本需要没有什么特别之处。
所有它需要做的就是等待其他API启动它,知道期望什么参数,有一个强大的错误报告机制,即保存以后可以查看它们的错误,或通过电子邮件发送给电子邮件你会经常看的帐户。
除此之外,通常的数据库访问代码应该是必要的。
当然,由于此过程中没有浏览器,因此您无法在浏览器中输入任何内容。