gpt4 book ai didi

php - 使用 str_ireplace 函数清理 xml 数据

转载 作者:行者123 更新时间:2023-11-29 19:22:22 26 4
gpt4 key购买 nike

当我运行脚本时,它在数据库中存储空白。我哪里错了。下面是 php 脚本:

<?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 />";
}
}

?>

我怀疑错误出在使用 str_ireplace 函数解析 xml 数据时。我查看了 PHP 文档,似乎我已经按照书本做了所有事情。

最佳答案

我建议您在这些时候使用 PDO:

$link= mysql_connect($server,$user,$pass) or die (mysql_error());
$conn= mysql_select_db($db) or die (mysql_error($link));

我建议这样做

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;
}
}

关于php - 使用 str_ireplace 函数清理 xml 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42369020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com