2017-02-21 40 views
-1

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

回答

0

對響應PayPal IPN的腳本沒有什麼特別的要求,比如調用。它需要做的只是等待其他API啓動它,知道期望的參數,具有強大的錯誤報告機制,即將錯誤保存在某處,稍後可以查看它們,或將它們發送給電子郵件帳戶,你會定期看。

除此之外,通常的數據庫訪問代碼都是應該需要的。

當然,你不能在瀏覽器中拋出任何東西,因爲在這個過程中沒有瀏覽器。

+0

謝謝。那麼你是說這個PHP和curl腳本足以獲得API的響應嗎? – tendaitakas

+0

我不得不承認,我不確定你爲什麼在那裏使用cURL,當然API會傳遞給你一些描述它告訴你的數據。所有你需要做的就是用這些信息修改你的數據庫 – RiggsFolly

+0

我正在使用curl發送我的json並得到以下響應:** $ result = curl_exec($ ch); ** – tendaitakas