Monday 3 September 2018

PHP Mysql Cleaning xml data using the str_ireplace function

When I ran the Script, it stores blanks in the database. Where am I going wrong. Below is the php script:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$request= <<<XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c2b="http://cps.huawei.com/cpsinterface/c2bpayment">
<soapenv:Header/>
<soapenv:Body>
  <c2b:C2BPaymentValidationRequest>
     <TransactionType>PayBill</TransactionType>
     <TransID>1234560000007031</TransID>
     <TransTime>20140227082020</TransTime>
     <TransAmount>123.00</TransAmount>
     <BusinessShortCode>12345</BusinessShortCode>
<BillRefNumber></BillRefNumber>
     <InvoiceNumber></InvoiceNumber>
<MSISDN>254722703614</MSISDN>
     <KYCInfo>
  <KYCName>[Personal Details][First Name]</KYCName>
  <KYCValue>Hoiyor</KYCValue>
</KYCInfo>
<KYCInfo>
  <KYCName>[Personal Details][Middle Name]</KYCName>
  <KYCValue>G</KYCValue>
</KYCInfo>
<KYCInfo>
  <KYCName>[Personal Details][Last Name]</KYCName>
  <KYCValue>Chen</KYCValue>
</KYCInfo>
  </c2b:C2BPaymentValidationRequest>
</soapenv:Body>
</soapenv:Envelope>
 XML;
//clean the soap input received from Mpesa so that you can parse it as raw XML

$clean_xml = str_replace(['soapenv:','c2b:' ],'', $request);
$xml = simplexml_load_string($clean_xml);
//you can extract any payment details using the below code
$server = '';
$user = '';
$pass = '';
$db  = ''; 

 foreach ($xml as $key => $cur)

 {
 //VALUES
  $AccountNo = $cur->BillRefNumber;
   $TransAmount = $cur->TransAmount;
  $TransID = $cur->TransID;
  $KYCInfo = $cur->KYCInfo;
  $MSISDN = $cur->MSISDN;                                                                                       

//SAVE TO DATABASE
$link= mysql_connect($server,$user,$pass) or die (mysql_error());
$conn= mysql_select_db($db) or die (mysql_error($link));
$query = "INSERT INTO c2b(TransID,MSISDN,BillRefNumber,KYCInfo,Amount) VALUES('$TransID','$MSISDN','$AccountNo','$KYCInfo','$TransAmount')";

if (!mysql_query($query))
{
die('Error: ' . mysql_error());
}
else
{
echo "New Records added successfully ! <br /><br />";
}
}                                                                                                                  

?>

I suspect that the error is in parsing the xml data using the str_ireplace function. I have looked at the PHP documentation and it seems that I have done everything by the book.

I suggest you use PDO at these points:
$link= mysql_connect($server,$user,$pass) or die (mysql_error());
$conn= mysql_select_db($db) or die (mysql_error($link));

I suggest this
class systemConfigs{
     public $conn;

     private $DBhost = 'localhost';
     private $DBname = 'mydbname';
     private $DBuser = 'dbuser';
     private $DBpwd  = 'dbpass';

     function __construct(){
        $this->dbConnect();
     }

     private function dbConnect(){
        $conn = null;
        try{
              $this->conn = new PDO("mysql:host=" . $this->DBhost  .";port=3306; dbname=" . $this->DBname, $this->DBuser, $this->DBpwd);
              $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
           }
           catch(PDOException $e){
              $conn = $e->getMessage();
           }

           return $conn;
       }

       /**
       * @param $sql
       * @return PDOStatement
       */
       public function runQuery($sql){
          $stmt = $this->conn->prepare($sql);
          return $stmt;
       }

       /**
       * @return string
       */
       public function lastID(){
         $stmt = $this->conn->lastInsertId();
         return $stmt;
       }

      public function insertC2B($TransID, $MSISDN, $AccNo, $KYCInfo, $Amount){
         $result = null;
         try{
               $sql = "INSERT INTO c2b(TransID,MSISDN,BillRefNumber,KYCInfo,Amount) VALUES(':TransID',':MSISDN',':AccountNo',':KYCInfo',':TransAmount')";
               $stmt = $this->runQuery($sql);
               $stmt->bindParam(':TransID', $TransID);
               $stmt->bindParam(':MSISDN', $MSISDN);
               $stmt->bindParam(':AccountNo', $AccountNo);
               $stmt->bindParam(':KYCInfo', $KYCInfo);
               $stmt->bindParam(':TransAmount', $Amount);
               $stmt->execute();

               $result = $this->lastID();
            } catch (PDOException $e){
               $result = $e->getMessage();
            }
            return $result;
         }
      }

0 comments:

Post a Comment