2011-04-04 21 views
0

我在將XML Webhook的內容放入PHP郵件腳本中時出現問題....出於某種原因,我無法訪問這些變量...我必須做某件事錯...SimpleXMLElement將XML放入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> 
+0

你怎麼不能訪問變量?什麼是錯誤? – 2011-04-04 21:54:18

回答

1

所有的XML節點看起來像他們正在正確地訪問和分配給PHP變量的值。您唯一的問題應該是$productTitlesarray_push()之前未初始化,如果error_reporting允許,它將拋出E_WARNING。

添加這些在上面看到PHP抱怨:

error_reporting(E_ALL) ; 
ini_set('display_errors', 1) ; 

然後添加此行之前foreach()解決它:

$productTitles = array() ; 

但是我沒有看到它被包含在電子郵件中文本。其他的東西沒有在電子郵件中顯示?