在php中使用Javascript

在php中使用Javascript,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我知道这个问题已经被问过好几次了,但我正在尝试将javascript与php结合使用。我有一个名为parsing.php的文件,它通过xml提要进行解析,并将元数据转换为名为“data”的JSON对象。解析是使用带有JavaScript和JQuery的ajax调用完成的 <script src="json2.js" type="text/javascript" language="javascript"></script> <script type="text/ja

我知道这个问题已经被问过好几次了,但我正在尝试将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);
  ?>"

$.ajax({
键入:“GET”,
url:'fakeFeed.xml',
数据类型:“xml”,
async:false,
成功:函数(数据、文本状态、jqXHR){
函数getRandom(最大值){
返回Math.floor(Math.random()*max);
}
函数getThumbId(小){
var num=getRandom(15);
如果(num==0){
num=1;
}
如果(数值<10){
num='0'+num;
}
返回num.toString();
}
var categories=new Array();//类别的数组
变量类别={
名称:“”,
视频:[]
}; 
var data1=数据;
风险值数据={
类别:[]
};
$(data1.find('item')。每个(函数(){
var el=$(本);
var categoryName=el.find('category').text();
var p=categories.indexOf(categoryName);
如果(p==-1){
categories.push(categoryName);
变量类别={
名称:categoryName,
视频:[]
}; 

对于(var j=0;j如果您真的想使用它而不是使用document.log for JS:

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

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

以下是我相信您正在努力实现的目标,用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);

您已经有一个AJAX请求,因此您了解AJAX的概念。您需要使用AJAX将解析的数据发送到服务器。或者,使用
DOMDocument
解析XML服务器端。此部分有错误:“window.data”“”
async:false,
请不要!因为PHP在服务器上运行,Javascript在客户端运行。JS永远无法直接调用PHP函数,反之亦然。你知道,JS也可以很好地解析XML。感谢@Alejandro的注释,但我无法实现这一点。当我检查some_PHP.PHP时,我会得到一个空白页面。我将ajax调用放在y你在我的ajax调用的结束括号后建议…将ajax放在一个函数中,并在原始ajax调用的末尾调用它谢谢你的帮助。我在那里添加视频只是作为填充数据。当我尝试运行此代码时,我在屏幕上找不到任何东西实际上我认为这可能是因为数组()。我将尝试更改它,看看它是否能做任何事情$data=array(“categories”=array())和$cat=array(“name”=>$name,“videos”=array())是数组的正确语法。很抱歉,php不是我的强项。如果是这样,我仍然无法让它工作。啊,我明白了,你还没有发现PHP5.4的神奇之处。)还有,其余的应该可以工作。你是否打开了
display_errors
?哈哈,是的。我仍然生活在php的黑暗时代。但是我得到了php解析错误:语法错误,第5行parsing2.php中意外的“[”,语法错误,意外的“=”,期望“')$dom->load(“fakeFeed.xml”);//这是第5行