douxiawei9318 2014-04-11 18:19
浏览 56

使用PHP解析XML以返回数字1的电子邮件

I am using a scorm quiz that can export data to my server as xml. I am trying to parse that data so that I can use some of it to send in a php email. What I am trying right now returns the number 1 and I can't figure out how to make it work. Help?

<?php

$version = $_POST['v'];
$points = $_POST['sp'];
$passing_percent = $_POST['psp'];
$gained_score = $_POST['tp'];
$username = $_POST['sn'];
$email = $_POST['se'];
$quiz_title = $_POST['qt'];
$detailed_results_xml = $_POST['dr'];

$xml_string = <<<_XML
print "<?xml version="1.0" encoding="iso-8859-1"?>" . $detailed_results_xml;
_XML;

$xml = simplexml_load_string($xml_string);
$emailtest = print_r($xml);

mail("email@email.com",$quiz_title,$emailtest,"From: fromname <noreply@test.com>");

?>

If I email the $detailed_results_xml to myself it comes through like this:

<quizReport xmlns="http://www.ispringsolutions.com/ispring/quizbuilder/quizresults"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.ispringsolutions.com/ispring/quizbuilder/quizresults  quizReport.xsd" version="1">
<quizSettings quizType="survey" timeLimit="0"/>
<summary time="32">
<variables>
  <variable name="USER_NAME" title="Name" value="mike"/>
  <variable name="USER_EMAIL" title="Email" value="test.test@test.com"/>
</variables>
</summary>
  <questions>
     <fillInTheBlankSurveyQuestion id="{BE7284DD-B731-409B-9198-A782499EECA9}" status="answered">
      <direction>Exercise 1: Current Sleep Habits (Night 1)
    In this exercise, think about the last few nights of sleep. Write the amount of time you slept per night and rate the quality of your sleep on a scale of 1 to 5, with 1 being bad and 5 being great. This will give you snapshot of your current sleep habits.
    Example:
    Day of Week
    Sunday
    Time to Bed At Night
    11:30 pm
    Time in Bed Before As!
    leep (estimate)
    1 hour
    Time Awake in the Morning
    6:30 am
    Quality of Sleep (1 to 5)
    3
    Total Time Sleeping
    6 hours </direction>
  <details>
    <text>
      <![CDATA[Day of Week]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time to Bed At Night]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time in Bed Before Asleep (estimate)]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time Awake in the Morning]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Quality of Sleep (1 to 5)]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Total Time Sleeping]]>
    </text>
    <blank userAnswer=""/>
  </details>
</fillInTheBlankSurveyQuestion>
<fillInTheBlankSurveyQuestion id="{F52AC41C-C149-4B8D-9976-5F380B1A7851}" status="answered">
  <direction>Exercise 1: Current Sleep Habits (Night 2)
    In this exercise, think about the last few nights of sleep. Write the amount of time you slept per night and rate the quality of your sleep on a scale of 1 to 5, with 1 being bad and 5 being great. This will give you snapshot o!
    f your current sleep habits.
    Example:
    Day of Week
    Sunday!

    Time to Bed At Night
    11:30 pm
    Time in Bed Before Asleep (estimate)
    1 hour
    Time Awake in the Morning
    6:30 am
    Quality of Sleep (1 to 5)
    3
    Total Time Sleeping
    6 hours </direction>
  <details>
    <text>
      <![CDATA[Day of Week]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time to Bed At Night]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time in Bed Before Asleep (estimate)]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time Awake in the Morning]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Quality of Sleep (1 to 5)]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Total Time Sleeping]]>
    </text>
    <blank userAnswer=""/>
  </details>
</fillInTheBlankSurveyQuestion>
<fillInTheBlankSurveyQuestion id="{4F33A341-308C-493E-BF98-E13927064BC5}" status="answered">
  <direction>Exercise 1: Current Sleep Habits (Night 3)
    In this exercise, think about the last few nights of sleep. Write the amount of time you slept per night and rate the quality of your sleep on a scale of 1 to 5, with 1 !
    being bad and 5 being great. This will give you snapshot of your current sleep habits.
    Example:
    Day of Week
    Sunday
    Time to Bed At Night
    11:30 pm
    Time in Bed Before Asleep (estimate)
    1 hour
    Time Awake in the Morning
    6:30 am
    Quality of Sleep (1 to 5)
    3
    Total Time Sleeping
    6 hours </direction>
  <details>
    <text>
      <![CDATA[Day of Week]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time to Bed At Night]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time in Bed Before Asleep (estimate)]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time Awake in the Morning]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Quality of Sleep (1 to 5)]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Total Time Sleeping]]>
    </text>
    <blank userAnswer=""/>
  </details>
</fillInTheBlankSurveyQuestion>
<fillInTheBlankSurveyQuestion id="{324BFC55-6E6D-48EF-9B0F-341131988317}" status="answered">
  <direction>Exercise 1: Current Sleep Habits (Night 4)
    In this exercise, think about the last few nights!
    of sleep. Write the amount of time you slept per night and rate the qu!
    ality of your sleep on a scale of 1 to 5, with 1 being bad and 5 being great. This will give you snapshot of your current sleep habits.
    Example:
    Day of Week
    Sunday
    Time to Bed At Night
    11:30 pm
    Time in Bed Before Asleep (estimate)
    1 hour
    Time Awake in the Morning
    6:30 am
    Quality of Sleep (1 to 5)
    3
    Total Time Sleeping
    6 hours </direction>
  <details>
    <text>
      <![CDATA[Day of Week]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time to Bed At Night]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time in Bed Before Asleep (estimate)]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time Awake in the Morning]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Quality of Sleep (1 to 5)]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Total Time Sleeping]]>
    </text>
    <blank userAnswer=""/>
  </details>
</fillInTheBlankSurveyQuestion>
<fillInTheBlankSurveyQuestion id="{96C36841-A582-4059-A408-8D8EA0CC09E6}" status="answered">
  <direction>Exercise 1: Current Sleep Habits (Night 5)
    !
    In this exercise, think about the last few nights of sleep. Write the amount of time you slept per night and rate the quality of your sleep on a scale of 1 to 5, with 1 being bad and 5 being great. This will give you snapshot of your current sleep habits.
    Example:
    Day of Week
    Sunday
    Time to Bed At Night
    11:30 pm
    Time in Bed Before Asleep (estimate)
    1 hour
    Time Awake in the Morning
    6:30 am
    Quality of Sleep (1 to 5)
    3
    Total Time Sleeping
    6 hours </direction>
  <details>
    <text>
      <![CDATA[Day of Week]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time to Bed At Night]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time in Bed Before Asleep (estimate)]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Time Awake in the Morning]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Quality of Sleep (1 to 5)]]>
    </text>
    <blank userAnswer=""/>
    <text>
      <![CDATA[ Total Time Sleeping]]>
    </text>
    <blank userAnswer=""/>
  </details>
</fillInTheBlankSurveyQuestion>
  <essayQuestion id="{C91C6866!-63A1-4BE5-84DD-1A0F8C34FB1F}" status="answered">
  <direction>Exercise 2:!
    Regulate the Body’s Clock through Daily Routine
    Some people use a morning routine to help them wake up and a bedtime routine of relaxing activities to prepare their bodies for sleep. These activities of daily living act as “cues†to signal the brain to fall into biological rhythms.
    In this exercise, consider for a minute what you did last night before bed and what you’ve done so far today.
    Take a moment to describe your daily routine and activities starting with last night.
    Evening activities at work, school, community or home: </direction>
  <userAnswer>asdfasdf</userAnswer>
</essayQuestion>
  <essayQuestion id="{AFE522F8-6D34-4434-BC41-61E7121615FD}" status="answered">
  <direction>Relaxing activities before bedtime: </direction>
  <userAnswer>asdfsasdf</userAnswer>
</essayQuestion>
  <essayQuestion id="{50E422B5-7705-49D6-9CFC-CF1E97389D10}" status="answered">
  <direction>
  Sleep Routine (time to bed, personal care activities, medication, and time, if awoken): </direction>
  <userAnswer>asdfsdfads</userAnswer>
</essayQuestion>
   <essayQuestion id="{6DF65956-6976-446F-87DD-02CD0A88FA14}" status="answered">
  <direction>Wake up routine (time awake, personal care activities, medication time): </direction>
  <userAnswer>asdfdfasdf</userAnswer>
</essayQuestion>
  <essayQuestion id="{CF38A3C2-E538-41B1-92C1-15011126231D}" status="answered">
  <direction>Morning activities at work, school, community or home: </direction>
  <userAnswer>asdfdfasdf</userAnswer>
</essayQuestion>
  <essayQuestion id="{CAA971E7-4722-4900-9667-4CF7B283A95C}" status="answered">
  <direction>Afternoon activities at work, school, community or home: </direction>
  <userAnswer>asdfasdf</userAnswer>
</essayQuestion>
<essayQuestion id="{2A64580E-A99C-4525-8435-2C57ECCBCEFB}" status="answered">
  <direction>Exercise 3: Planning for Improvement
    In this brief exercise, you’ll consider all that you’ve come to realize about your own habits and routine and take a moment to reflect on some small changes that might!
    benefit you.
    Use the space below to make a short list of small chang!
    es you can make to your own routine that might prevent sleep loss. As you’re doing this, consider some of the following questions:
    What changes, if any, have I made in the past that have worked for me? </direction>
  <userAnswer>asdfsad</userAnswer>
</essayQuestion>
<essayQuestion id="{6EB6CCF7-045A-4E8B-AFA0-794F00FEB658}" status="answered">
  <direction>Are there some changes that have not worked? </direction>
  <userAnswer>asdfsasdf</userAnswer>
</essayQuestion>
<essayQuestion id="{99CBF95D-7427-4999-BF93-6D727D607B0A}" status="answered">
  <direction>A few small changes I can make to improve my sleep and health: </direction>
  <userAnswer>asdfsasdf</userAnswer>
</essayQuestion>
<essayQuestion id="{E3621400-19BD-4200-A8A2-DE6544EFAD65}" status="answered">
  <direction>If I make this change, how might it impact my life/Life Goals? </direction>
  <userAnswer>asdfasdfa</userAnswer>
</essayQuestion>

Sorry if the formatting got messed up on the xml bit but you should get the idea from it I think. So the xml bit is what gets generated by $detailed_results_xml and I need to parse some pieces of it to html to send in an email, but right now I am only seeing a number 1. Any help is greatly appreciated!

  • 写回答

1条回答 默认 最新

  • drj58429 2014-04-11 18:23
    关注

    print_r return 1 (as integer). You must pass a second boolean parameter for it to return a string.

    the line is:

    $emailtest = print_r($xml);
    

    That is why you're getting 1 as the $message parameter for mail. And probably you are sending the wrong message (email() 3rd parameter), I think you wold want to:

    mail("email@email.com", $quiz_title, $xml, "From: fromname <noreply@test.com>");
    

    For parsing the XML there are many ways :

    functions

    object oriented

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?