我在将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 = 'firstemail@hotmail.com';
// 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 <myemail@hotmail.com>' . "\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>bob@customer.com</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>
答案 0 :(得分:1)
所有xml节点看起来都被正确访问,并且它们的值分配给PHP变量。您唯一的问题应该是$productTitles
在array_push()
之前未初始化,如果error_reporting允许,则会抛出E_WARNING。
将这些添加到顶部以查看PHP抱怨:
error_reporting(E_ALL) ;
ini_set('display_errors', 1) ;
然后在foreach()
之前添加此行以修复它:
$productTitles = array() ;
但我不认为它包含在电子邮件文本中。电子邮件中没有显示其他内容吗?