XML 到 PHP 变量,然后到 Flashvars

发布于 2025-01-01 22:00:15 字数 3306 浏览 3 评论 0原文

是否可以使用 php 读取 XML 文件,然后将该数据传递给 Flashvars?

在寻找解决方案时,我只能找到如何将 XML 指向文件。目前,我正在打开 XML 文件,将其分配给 PHP 变量,对其进行调整,然后将其写入文件,然后由 Flashvars 拾取该文件。这在 99% 的情况下都有效,但有时结果是完全错误的!我无法以我目前的知识调试这个问题。

如果我可以将数据作为字符串而不是文件传递,那就容易多了。

这是我当前的 PHP 代码...

    <?php
    $homepage = file_get_contents('content.xml');

    // Search and replace text in $homepage

    $myFile = "testFile.txt";
    $ourFileHandle = fopen($myFile, 'w') or die("can't open file");
    fwrite($ourFileHandle, '');
    fwrite($ourFileHandle, $homepage);
    fclose($ourFileHandle);
    ?>

    <script type="text/javascript" src="swfobject/swfobject.js"></script>
    <script type="text/javascript">
        var flashvars = {};
        flashvars.pathToXML = "testFile.txt";
        var params = {wmode: "transparent"};
        var attributes = {};
        swfobject.embedSWF("flashfile.swf", "myAlternativeContent", "660", "120", "9.0.0", false, flashvars, params, attributes);
    </script>

这是我的 XML...

    <root>
        <config>
            <columns>6</columns>
            <padding>8</padding>
            <horizontal_align>true</horizontal_align>
            <vertical_align>true</vertical_align>
            <scale>0.7</scale>
        </config>

        <loop>
            <title>6th (bottom)</title>
            <path>sounds/string6</path>
            <key>name6</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>
        <loop>
            <title>5th</title>
            <path>sounds/string5</path>
            <key>name5</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>
        <loop>
            <title>4th</title>
            <path>sounds/string4</path>
            <key>name4</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>
        <loop>
            <title>3rd</title>
            <path>sounds/string3</path>
            <key>name3</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>
        <loop>
            <title>2nd</title>
            <path>sounds/string2</path>
            <key>name2</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>
        <loop>
            <title>1st (top)</title>
            <path>sounds/string1</path>
            <key>name1</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>     
    </root>

Is it possible to use php to read an XML file and then pass that data to Flashvars?

Upon searching for a solution I can only find how to point the XML to a file. Currently I am opening the XML file, assigning it to a PHP variable, adjusting it and then writing it to a file which is then picked up by Flashvars. This works 99% of the time, but sometimes the results are totally wrong! I cannot with my current knowledge debug this problem.

It would be so much easier if I could just pass the data as a string instead of a file.

Here is my current PHP code...

    <?php
    $homepage = file_get_contents('content.xml');

    // Search and replace text in $homepage

    $myFile = "testFile.txt";
    $ourFileHandle = fopen($myFile, 'w') or die("can't open file");
    fwrite($ourFileHandle, '');
    fwrite($ourFileHandle, $homepage);
    fclose($ourFileHandle);
    ?>

    <script type="text/javascript" src="swfobject/swfobject.js"></script>
    <script type="text/javascript">
        var flashvars = {};
        flashvars.pathToXML = "testFile.txt";
        var params = {wmode: "transparent"};
        var attributes = {};
        swfobject.embedSWF("flashfile.swf", "myAlternativeContent", "660", "120", "9.0.0", false, flashvars, params, attributes);
    </script>

And here is my XML...

    <root>
        <config>
            <columns>6</columns>
            <padding>8</padding>
            <horizontal_align>true</horizontal_align>
            <vertical_align>true</vertical_align>
            <scale>0.7</scale>
        </config>

        <loop>
            <title>6th (bottom)</title>
            <path>sounds/string6</path>
            <key>name6</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>
        <loop>
            <title>5th</title>
            <path>sounds/string5</path>
            <key>name5</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>
        <loop>
            <title>4th</title>
            <path>sounds/string4</path>
            <key>name4</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>
        <loop>
            <title>3rd</title>
            <path>sounds/string3</path>
            <key>name3</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>
        <loop>
            <title>2nd</title>
            <path>sounds/string2</path>
            <key>name2</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>
        <loop>
            <title>1st (top)</title>
            <path>sounds/string1</path>
            <key>name1</key>
            <volume>0.6</volume>
            <panning>0</panning>
            <offset>0</offset>
        </loop>     
    </root>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡紫姑娘! 2025-01-08 22:00:15

我不久前做了类似的东西。希望它可以帮助你。

 <?php


    function extractXML($url){
      $doc = new DOMDocument();
      $doc->load($url);
      $arrFeeds = array();
      foreach ($doc->getElementsByTagName('loop') as $node) {
        $itemRSS = array ( 
          'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'path' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'key' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'volume' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'panning' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'offset' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
          );
        array_push($arrFeeds, $itemRSS);
      }
        return $arrFeeds;
    }


    $tabXmlData = extractXML("YOURLINK");


    for($i = 0; $i < (int)$feedAmount; $i++){
        if(isset($tabXmlData[$i])){
            $flashVars .= "INSERT WHAT NEEDED";
        }
    }


    ?>

<script type="text/javascript" src="swfobject/swfobject.js"></script>
<script type="text/javascript">
    var flashvars = {<?php echo $flashVars ?>};
    flashvars.pathToXML = "testFile.txt";
    var params = {wmode: "transparent"};
    var attributes = {};
    swfobject.embedSWF("flashfile.swf", "myAlternativeContent", "660", "120", "9.0.0", false, flashvars, params, attributes);
</script>

澄清:
该函数基本上将 XML 存储在具有关联键的数组中。之后您需要做的就是获取所需的参数并将它们以正确的格式存储到 flashvars 中。

PS:如果您需要替换字符串值,请检查 str_replace()preg_replace()

i made something similar a while ago. hope it can help you.

 <?php


    function extractXML($url){
      $doc = new DOMDocument();
      $doc->load($url);
      $arrFeeds = array();
      foreach ($doc->getElementsByTagName('loop') as $node) {
        $itemRSS = array ( 
          'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'path' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'key' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'volume' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'panning' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'offset' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
          );
        array_push($arrFeeds, $itemRSS);
      }
        return $arrFeeds;
    }


    $tabXmlData = extractXML("YOURLINK");


    for($i = 0; $i < (int)$feedAmount; $i++){
        if(isset($tabXmlData[$i])){
            $flashVars .= "INSERT WHAT NEEDED";
        }
    }


    ?>

<script type="text/javascript" src="swfobject/swfobject.js"></script>
<script type="text/javascript">
    var flashvars = {<?php echo $flashVars ?>};
    flashvars.pathToXML = "testFile.txt";
    var params = {wmode: "transparent"};
    var attributes = {};
    swfobject.embedSWF("flashfile.swf", "myAlternativeContent", "660", "120", "9.0.0", false, flashvars, params, attributes);
</script>

Clarifications :
the function is basically storing your XML in an array with associative keys. all you need to do after that is take the params you want and store them in the correct format for flashvars.

PS: if you need to replace strings value go check str_replace() or preg_replace()

锦爱 2025-01-08 22:00:15

这就是你要做的:

下载 xml2array,它对所有 php / xml 应用程序非常有帮助 http:// www.bin-co.com/php/scripts/xml2array/

将其包含在您的页面上。

然后使用以下几行:

$homepage = file_get_contents('content.xml');

$xml_array = xml2array($homepage);

如果您愿意,您可以做尽可能少的事情:要么使用explode()使其成为像您所说的字符串,您可以对数组进行json_encode(),然后在Javascript块中回显它,或者只是在页面上输出代码,如下所示:

param.something = <?= $xml_array['my_key']; ?>;

Here's what you do:

Download xml2array, very helpful in all php / xml apps http://www.bin-co.com/php/scripts/xml2array/

Include it on your page.

Then use the following lines:

$homepage = file_get_contents('content.xml');

$xml_array = xml2array($homepage);

You can do as few things if you want: either use explode() to make it a string like you had said, you can json_encode() the array and then echo that out in your Javascript block, or just output the code on the page like so:

param.something = <?= $xml_array['my_key']; ?>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文