我需要为opencart创建付款模块。如何在php中构建xml输出以将其发布到远程租赁服务器URL的问题:https://ecredit.leasing.com/eshop_test/UBLOnline.aspx?eshopdata=?
使用post方法。 我试图构建这个xml:
<?xml version="1.0" encoding="UTF-8"?>
<ContractRequest xmlns="http://e-credit.ubl.lt/eshop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://e-credit.ubl.lt/eshop http://e-credit.leasing.com/eshop/contractrequest.xsd">
<SaleLogin>test</SaleLogin>
<SaleConditionID>111</SaleConditionID>
<Person>
<FirstName>John</FirstName>
<LastName>Jones</LastName>
</Person>
<Communication>
<StreetOrVillage>First</StreetOrVillage>
<City>New York</City>
<PostCode>21212</PostCode>
<Phone>+100000000</Phone>
<Email>john@mymail.com</Email>
</Communication>
<ContractDetails>
<CreditAmount>617.60</CreditAmount>
<CreditCurrency>USD</CreditCurrency>
</ContractDetails>
<OrderedItemsDetail>
<Item>
<Name>1 x HP 250 G5 UMA Celeron N3060 15.6 HD SVA, 1 x HP 15-r101na 15.6 HD Brightview flat (B), </Name>
<Amount>2pcs.</Amount>
<Price>617.60</Price>
<Currency>USD</Currency>
</Item>
</OrderedItemsDetail>
</ContractRequest>
我试过像这样创建xml:
$XmlString = '<?xml version="1.0" encoding="UTF-8" ?>';
$XmlString .= '<ContractRequest
xmlns="http://e-credit.ubl.lt/eshop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://e-credit.ubl.com/eshop http://e-credit.leasing.com/eshop/contractrequest.xsd">';
$XmlString .= '<SaleLogin>' . $data['SaleLogin'] . '</SaleLogin>';
$XmlString .= '<SaleConditionID>' . $data['SaleConditionID'] . '</SaleConditionID>';
$XmlString .= '<Person>';
$XmlString .= '<FirstName>' . $data['first_name'] . '</FirstName>';
$XmlString .= '<LastName>' . $data['last_name'] . '</LastName>';
$XmlString .= '</Person>';
$XmlString .= '<Communication>';
$XmlString .= '<StreetOrVillage>' . $data['street'] . '</StreetOrVillage>';
$XmlString .= '<City>' . $data['city'] . '</City>';
$XmlString .= '<PostCode>' . $data['postcode'] . '</PostCode>';
$XmlString .= '<Phone>' . $data['telephone'] . '</Phone>';
$XmlString .= '<Email>' . $data['email'] . '</Email>';
$XmlString .= '</Communication>';
$XmlString .= '<ContractDetails>';
$XmlString .= '<CreditAmount>' . $data['amount'] . '</CreditAmount>';
$XmlString .= '<CreditCurrency>' . $data['currency'] . '</CreditCurrency>';
$XmlString .= '</ContractDetails>';
$XmlString .= '<OrderedItemsDetail>';
$XmlString .= '<Item>';
$XmlString .= '<Name>' . $data['description'] . '</Name>';
$XmlString .= '<Amount>' . $data['products_amount'] . '</Amount>';
$XmlString .= '<Price>' . $data['amount'] . '</Price>';
$XmlString .= '<Currency>' .$data['currency'] . '</Currency>';
$XmlString .= '</Item>';
$XmlString .= '</OrderedItemsDetail>';
$XmlString .= '</ContractRequest>';
之后:
$url = https://ecredit.leasing.com/eshop_test/UBLOnline.aspx?eshopdata=?;
$header = array();
$header[] = 'Content-Type: text/xml';
$header[] = 'Content-Length: ' . strlen($XmlString);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, &url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $XmlString);
///////////////////////
$response = curl_exec($ch);
curl_close($ch);
$XmlString = simplexml_load_string($response);
//create object to use as json
$json = array();
$json['redirect'] = $this->url->link('checkout/success', '', true);
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
但这不起作用......伤心......我想念的是什么?
如何正确创建xml请求并将其发布到url?
答案 0 :(得分:2)
您已经在评论中提供了一些提示(也许是您的答案),但这里有一些关于您应该如何改进代码并帮助其他人帮助您的想法。
PHP为您提供了一些创建XML的好资源,您应该使用它们。我推荐DOMDocument。以下是您的示例的入门代码。
$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$namespace = 'http://e-credit.ubl.lt/eshop';
$contractRequest = $dom->createElement('ContractRequest');
$contractRequest->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', $namespace);
$saleLogin = $dom->createElement('SaleLogin', 'test');
$saleConditionID = $dom->createElement('SaleConditionID', '111');
$contractRequest->appendChild($saleLogin);
$contractRequest->appendChild($saleConditionID);
$dom->appendChild($contractRequest);
//you got the idea...
对于卷曲部分,首先将第一个setopt更改为curl_setopt($ch, CURLOPT_URL, $url);
。现在,在关闭curl句柄之前,插入以下代码:
if($response === false)
{
echo 'Curl error: ' . curl_error($ch);
}
如果不起作用,您可以调试curl请求。这another question可能会对您有所帮助。
更新
我认为对你的请求使用curl是正确的选择,但是,正如你在评论中所要求的,如果你的curl请求仍然失败,这里有一个选项。
$payload = array();
$payload['header'] = 'Content-Type: text/xml';
$payload['header'] .= 'Content-Length: ' . strlen($XmlString);
$payload['content'] = is_array($XmlString) ? http_build_query($XmlString) : $XmlString;
$payload['method'] = 'POST';
$payloadContext = stream_context_create(array('http' => $payload));
$result = file_get_contents($url, false, $payloadContext);
if ($result === false) {
print_r ("Error in your request. Check logs for more information");
}
正如我所说,它只是一个选项,但卷曲仍然很好,你只需要纠正它。