我是PHP web服务的新手,之前我问过这个问题,但无法获得有用的解决方案,因此我试图重新编写它。我试图听API的响应,并更新我的数据库,就像PayPal IPN服务所做的一样。当我在浏览器中运行脚本时,至今为止我的脚本运行良好,并且我通过POST将我的json响应回传,但后来API发送了另一个响应而没有浏览/客户端的参与,我希望它能够进行通信我的PHP脚本在服务器和更新数据库。 API有一个notifyURL字段,它需要它发送这些通知,并且我已将它设置为发送请求的同一个文件。以下是我到目前为止的代码:使用php和curl创建响应式监听服务,就像paypal IPN
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}"
谢谢。那么你是说这个PHP和curl脚本足以获得API的响应吗? – tendaitakas
我不得不承认,我不确定你为什么在那里使用cURL,当然API会传递给你一些描述它告诉你的数据。所有你需要做的就是用这些信息修改你的数据库 – RiggsFolly
我正在使用curl发送我的json并得到以下响应:** $ result = curl_exec($ ch); ** – tendaitakas