我是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