将Javascript与php结合使用

Using Javascript with php

本文关键字:结合 php Javascript      更新时间:2024-02-27

我知道这个问题已经被问过几次了,但我正在尝试将javascript与php结合使用。我有一个名为parsing.php的文件,它通过xml提要进行解析,并将元数据转换为名为"data"的JSON对象。解析是使用带有JavaScript和JQuery的ajax调用来完成的。

<script src="json2.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript">
$.ajax({
 type: 'GET',
 url: 'fakeFeed.xml',
 dataType: 'xml',
 async: false,
success: function(data, textStatus, jqXHR) {
function getRandom(max) {
  return Math.floor(Math.random() * max);
}
function getThumbId(small) {
  var num = getRandom(15);
  if (num == 0) {
    num = 1;
  }
  if (num < 10) {
    num = '0' + num;
  }
  return num.toString();
}
var categories = new Array();  // Array for the categories
var category = {
  name : '',
  videos: []
}; 
var data1 = data;
var data = {
  categories: []
};
$(data1).find('item').each(function () { 
  var el = $(this);
  var categoryName = el.find('category').text();
  var p = categories.indexOf(categoryName);
  if( p == -1) {
    categories.push(categoryName);
    var category = {
      name: categoryName,
      videos: []
    }; 
    for (var j = 0; j<5; j++) {
      var video = {
        sources: [el.find('media'':content, content').attr('url')],
        thumb : 'images'/thumbs'/thumb' + getThumbId() + '.jpg',
        title : el.find("title").text(),
        subtitle : el.find("description").text(),
        description: ""  
      }
      category.videos.push(video);
    }
    data.categories.push(category);
  }
});
window.data = JSON.stringify(data);
 <script>
 "<?php
   $dataVar = ?> <script type=text/javascript>window.data</script><?php;?>" 
 "<?php
   print_r($dataVar,true);
  ?>"

我需要使用javascript和php的唯一原因是,我想使用php中的"print_r()"函数,它允许我返回信息,而不仅仅是将信息打印到屏幕上,但不幸的是,我无法使其工作。如果有人知道其他选择,或者能提供一些建议,我们将不胜感激。

以下是我相信您正在努力实现的目标,用PHP编写:

$dom = new DOMDocument();
$dom->load("fakeFeed.xml");
$data = ["categories"=>[]]; // may need to use array() instead of [] depending on PHP version
foreach($dom->getElementsByTagName('item') as $item) {
    $name = trim($item->getElementsByTagName('category')->item(0)->textContent);
    if( !isset($data['categories'][$name])) {
        $cat = ["name"=>$name,"videos"=>[]]; // again, adjust if you're on an older version
        // I'm not entirely sure what you're trying to achieve on this part.
        // you seem to be getting the same video five times...
        // revise your approach, comment if needed, and I can try to help
        // for now, "insert code here"
        $data['categories'][$name] = $cat;
    }
}
// we were using the name as a key for simplicity, now just take the values
$data['categories'] = array_values($data['categories']);
// done! $data now has your data.
var_dump($data);

如果您真的想使用这个,而不是对JS:使用document.log

$.ajax({
                    type: "POST",
                    url: "some_php.php",
                    data: JSON.stringify(data);
                })
                    .done(function( msg ) {
                        document.write(msg);
                    });

和some_php.php

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);