SimpleXMLElement 帮助将 XML 放入 PHP Mail 中
我在将 XML webhook 的内容放入 PHP 邮件脚本时遇到问题...由于某种原因我无法访问变量...我一定做错了什么...
任何人都可以帮忙吗?
// Get XML data and read it into a string for use with SimpleXML
$xmlData = fopen('php://input' , 'rb');
while (!feof($xmlData)) { $xmlString .= fread($xmlData, 4096); }
fclose($xmlData);
$xml = new SimpleXMLElement($xmlString);
// Extract the name, email, country
$user_email = trim($xml->email);
$user_first_name = trim($xml->{'billing-address'}->{'first-name'});
$user_last_name = trim($xml->{'billing-address'}->{'last-name'});
$user_address1 = trim($xml->{'billing-address'}->address1);
$user_address2 = trim($xml->{'billing-address'}->address2);
$user_phone = trim($xml->{'billing-address'}->phone);
$user_province = trim($xml->{'billing-address'}->province);
$user_zip = trim($xml->{'billing-address'}->zip);
$user_city = trim($xml->{'billing-address'}->city);
$user_country = trim($xml->{'billing-address'}->country);
$date = trim($xml->{'order'}->{'created-at'});
foreach ($xml->{'line-items'}->{'line-item'} as $lineItem) {
array_push($productTitles, trim($lineItem->title));
}
// multiple recipients
$to = '[email protected]';
// subject
$subject = 'myemail Purchase - '.$user_first_name.' '.$user_last_name.'';
$message = '
<html>
<head>
<title>myemail Purchase</title>
</head>
<body>
<p>Here are the details</p>
<table><tr>';
$message .= '<td>'.$user_first_name.' '.$user_last_name.'</td>';
$message .= '<td>';
$message .= '</td>';
$message .= '<td>'.$date.'</td>';
$message .= '</tr></table></body></html>';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Charles <[email protected]>' . "\r\n";
$headers .= 'From: myemail.com <myemail.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
这是我得到的 XML...
<?xml version="1.0" encoding="UTF-8"?>
<order>
<buyer-accepts-marketing type="boolean">false</buyer-accepts-marketing>
<cart-token nil="true"></cart-token>
<closed-at type="datetime" nil="true"></closed-at>
<created-at type="datetime">2005-07-31T15:57:11Z</created-at>
<currency>USD</currency>
<email>[email protected]</email>
<financial-status>paid</financial-status>
<fulfillment-status nil="true"></fulfillment-status>
<gateway>bogus</gateway>
<id type="integer">516163746</id>
<note nil="true"></note>
<number type="integer">1</number>
<shop-id type="integer">820947126</shop-id>
<subtotal-price type="integer">10.00</subtotal-price>
<taxes-included type="boolean">false</taxes-included>
<total-discounts type="integer">0.00</total-discounts>
<total-line-items-price type="integer">10.00</total-line-items-price>
<total-price type="integer">11.50</total-price>
<total-tax type="integer">1.50</total-tax>
<total-weight type="integer">0</total-weight>
<updated-at type="datetime">2005-08-01T15:57:11Z</updated-at>
<name>#1001</name>
<billing-address>
<address1>123 Amoebobacterieae St</address1>
<address2></address2>
<city>Ottawa</city>
<company></company>
<country>Canada</country>
<first-name>Bob</first-name>
<id type="integer">943494288</id>
<last-name>Bobsen</last-name>
<phone>(555)555-5555</phone>
<province>Ontario</province>
<shop-id type="integer">820947126</shop-id>
<zip>K2P0V6</zip>
<name>Bob Bobsen</name>
</billing-address>
<shipping-address>
<address1>123 Amoebobacterieae St</address1>
<address2></address2>
<city>Ottawa</city>
<company></company>
<country>Canada</country>
<first-name>Bob</first-name>
<id type="integer">943494288</id>
<last-name>Bobsen</last-name>
<phone>(555)555-5555</phone>
<province>Ontario</province>
<shop-id type="integer">820947126</shop-id>
<zip>K2P0V6</zip>
<name>Bob Bobsen</name>
</shipping-address>
<line-items type="array">
<line-item>
<fulfillment-service>manual</fulfillment-service>
<grams type="integer">1500</grams>
<id type="integer">642703538</id>
<price type="integer">10.00</price>
<quantity type="integer">1</quantity>
<sku>1</sku>
<title>Draft</title>
<variant-id type="integer">47052976</variant-id>
<vendor nil="true"></vendor>
<name>Draft - 151cm</name>
</line-item>
</line-items>
</order>
I'm having problems putting the contents of my XML webhook into a PHP mail script.... For some reason I can't access the variables... I must be doing something wrong...
Can anyone help?
// Get XML data and read it into a string for use with SimpleXML
$xmlData = fopen('php://input' , 'rb');
while (!feof($xmlData)) { $xmlString .= fread($xmlData, 4096); }
fclose($xmlData);
$xml = new SimpleXMLElement($xmlString);
// Extract the name, email, country
$user_email = trim($xml->email);
$user_first_name = trim($xml->{'billing-address'}->{'first-name'});
$user_last_name = trim($xml->{'billing-address'}->{'last-name'});
$user_address1 = trim($xml->{'billing-address'}->address1);
$user_address2 = trim($xml->{'billing-address'}->address2);
$user_phone = trim($xml->{'billing-address'}->phone);
$user_province = trim($xml->{'billing-address'}->province);
$user_zip = trim($xml->{'billing-address'}->zip);
$user_city = trim($xml->{'billing-address'}->city);
$user_country = trim($xml->{'billing-address'}->country);
$date = trim($xml->{'order'}->{'created-at'});
foreach ($xml->{'line-items'}->{'line-item'} as $lineItem) {
array_push($productTitles, trim($lineItem->title));
}
// multiple recipients
$to = '[email protected]';
// subject
$subject = 'myemail Purchase - '.$user_first_name.' '.$user_last_name.'';
$message = '
<html>
<head>
<title>myemail Purchase</title>
</head>
<body>
<p>Here are the details</p>
<table><tr>';
$message .= '<td>'.$user_first_name.' '.$user_last_name.'</td>';
$message .= '<td>';
$message .= '</td>';
$message .= '<td>'.$date.'</td>';
$message .= '</tr></table></body></html>';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Charles <[email protected]>' . "\r\n";
$headers .= 'From: myemail.com <myemail.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
Here is the XML I'm getting...
<?xml version="1.0" encoding="UTF-8"?>
<order>
<buyer-accepts-marketing type="boolean">false</buyer-accepts-marketing>
<cart-token nil="true"></cart-token>
<closed-at type="datetime" nil="true"></closed-at>
<created-at type="datetime">2005-07-31T15:57:11Z</created-at>
<currency>USD</currency>
<email>[email protected]</email>
<financial-status>paid</financial-status>
<fulfillment-status nil="true"></fulfillment-status>
<gateway>bogus</gateway>
<id type="integer">516163746</id>
<note nil="true"></note>
<number type="integer">1</number>
<shop-id type="integer">820947126</shop-id>
<subtotal-price type="integer">10.00</subtotal-price>
<taxes-included type="boolean">false</taxes-included>
<total-discounts type="integer">0.00</total-discounts>
<total-line-items-price type="integer">10.00</total-line-items-price>
<total-price type="integer">11.50</total-price>
<total-tax type="integer">1.50</total-tax>
<total-weight type="integer">0</total-weight>
<updated-at type="datetime">2005-08-01T15:57:11Z</updated-at>
<name>#1001</name>
<billing-address>
<address1>123 Amoebobacterieae St</address1>
<address2></address2>
<city>Ottawa</city>
<company></company>
<country>Canada</country>
<first-name>Bob</first-name>
<id type="integer">943494288</id>
<last-name>Bobsen</last-name>
<phone>(555)555-5555</phone>
<province>Ontario</province>
<shop-id type="integer">820947126</shop-id>
<zip>K2P0V6</zip>
<name>Bob Bobsen</name>
</billing-address>
<shipping-address>
<address1>123 Amoebobacterieae St</address1>
<address2></address2>
<city>Ottawa</city>
<company></company>
<country>Canada</country>
<first-name>Bob</first-name>
<id type="integer">943494288</id>
<last-name>Bobsen</last-name>
<phone>(555)555-5555</phone>
<province>Ontario</province>
<shop-id type="integer">820947126</shop-id>
<zip>K2P0V6</zip>
<name>Bob Bobsen</name>
</shipping-address>
<line-items type="array">
<line-item>
<fulfillment-service>manual</fulfillment-service>
<grams type="integer">1500</grams>
<id type="integer">642703538</id>
<price type="integer">10.00</price>
<quantity type="integer">1</quantity>
<sku>1</sku>
<title>Draft</title>
<variant-id type="integer">47052976</variant-id>
<vendor nil="true"></vendor>
<name>Draft - 151cm</name>
</line-item>
</line-items>
</order>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有 xml 节点看起来都被正确访问,并且它们的值已分配给 PHP 变量。您唯一的问题应该是
$productTitles
在array_push()
< 之前未初始化/a>,如果 error_reporting 允许,它将抛出 E_WARNING。将这些添加到顶部以查看 PHP 抱怨:
然后在
foreach()
之前添加此行来修复它:但是我没有看到它包含在电子邮件文本中。电子邮件中是否还有其他内容未显示?
All the xml nodes look like they're being accessed correctly and their values assigned to PHP variables. Your only issue should be
$productTitles
being uninitialized before thearray_push()
, which will throw an E_WARNING if error_reporting allows for it.Add these on top to see PHP complaining:
Then add this line before the
foreach()
to fix it:However I don't see it being included in the email text. Is something else not being showed in the email?