gpt4 book ai didi

javascript - XML 到 JSON 代理服务器未获取 XML 源

转载 作者:行者123 更新时间:2023-11-28 18:38:48 25 4
gpt4 key购买 nike

我正在尝试使用 WordPress 提供的 RSS 提要将 WordPress 博客中的帖子显示到远程网站上。但是,当然,我必须尝试绕过 CORS。因此,我现在尝试在 PHP 中创建 XML 到 JSON 代理服务器,并使用 this resource as a guide .

Here's how I have my HTML set up for the output:

<div id="output"></div>

The ajax to call the PHP proxy:

// my test XML feed with only one post for now
var url = "http://www.intecllc.net/wp/feed/";

// AJAX request
var xhr = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
xhr.onreadystatechange = XHRhandler;
xhr.open("GET", "xmlproxy.php?url=" + escape(url), true);
xhr.send(null);

// handle response
function XHRhandler() {
if (xhr.readyState == 4) {
// parse response as JSON
var json;
if (JSON && JSON.parse) {
json = JSON.parse(xhr.responseText);
}
else {
eval("var json = " + xhr.responseText);
}
Display(json);
xhr = null;
}
}

// display post(s)
function Display(data) {
var output = document.getElementById("output");
Show("Data from URL: "+url);

if (data && data.item) {
if (data.item.length) {
// multiple statuses
for (var i=0, sl=data.item.length; i < sl; i++) {
Show(data.item[i]);
}
}
else {
// single status
Show(data.item);
}
}

// display item
function Show(item) {
if (typeof item != "string") {
item = item.title + ": " + item.description;
}
var p = document.createElement("p");
p.appendChild(document.createTextNode(item));
output.appendChild(p);
}
}

The PHP proxy code (The proxy will fetch the contents of the passed URL as a string, parse it as XML and convert it to JSON. The JSON string will be returned to the calling JavaScript process.):

<?php 

ini_set('display_errors', false);
set_exception_handler('ReturnError');
$r = '';
$url = (isset($_GET['url']) ? $_GET['url'] : null);

if ($url) {
// fetch XML
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_TIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true
));
$r = curl_exec($c);
curl_close($c);
}

if ($r) {
// XML to JSON
echo json_encode(new SimpleXMLElement($r));
}
else {
// nothing returned?
ReturnError();
}

// return JSON error flag
function ReturnError() {
echo '{"error":true}';
}

不幸的是,它没有抓取提要并解析它。有人可以帮我解决问题吗?谢谢。

最佳答案

我认为您的输出 JSON 不是您所期望的。我从同一个 feed 中获取以下 JSON

{
"@attributes": {
"version": "2.0"
},
"channel": {
"title": "InTec, LLC",
"link": "http://www.intecllc.net/wp",
"description": "tagline here",
"lastBuildDate": "Fri, 04 Mar 2016 17:47:52 +0000",
"language": "en-US",
"generator": "http://wordpress.org/?v=4.2.7",
"item": {
"title": "InTec Welcomes New Director of Contracts",
"link": "http://www.intecllc.net/wp/intec-welcomes-new-director-of-contracts/",
"comments": "http://www.intecllc.net/wp/intec-welcomes-new-director-of-contracts/#comments",
"pubDate": "Fri, 04 Mar 2016 17:47:52 +0000",
"category": {},
"guid": "http://www.intecllc.net/wp/?p=101",
"description": {}
}
}
}

在这里我们可以清楚地看到没有名为 item 的键立即位于根元素,因此您的 if状况if (data && data.item) {无法满足。

这是修改后的 JavaScript,

    // my test XML feed with only one post for now
var url = "http://www.intecllc.net/wp/feed/";

// AJAX request
var xhr = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
xhr.onreadystatechange = XHRhandler;
xhr.open("GET", "index.php?url=" + escape(url), true);
xhr.send(null);

// handle response
function XHRhandler() {
if (xhr.readyState == 4) {
// parse response as JSON
var json;
if (JSON && JSON.parse) {
json = JSON.parse(xhr.responseText);
} else {
eval("var json = " + xhr.responseText);
}
Display(json);
xhr = null;
}
}

// display post(s)
function Display(data) {
var output = document.getElementById("output");
Show("Data from URL: " + url);

if (data && data.channel.item) {
if (data.channel.item.length) {
// multiple statuses
for (var i = 0, sl = data.channel.item.length; i < sl; i++) {
Show(data.channel.item[i]);
}
} else {
// single status
Show(data.channel.item);
}
}

// display item
function Show(item) {
if (typeof item != "string") {
item = item.title + ": " + item.description;
}
var p = document.createElement("p");
p.appendChild(document.createTextNode(item));
output.appendChild(p);
}
}

希望这是您正在寻找的

此外,您需要替换 PHP 代码中的以下内容

echo json_encode(new SimpleXMLElement($r));

echo json_encode(simplexml_load_string($r, 'SimpleXMLElement', LIBXML_NOCDATA));

这将更好地处理 <![CDATA[节点

关于javascript - XML 到 JSON 代理服务器未获取 XML 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36498090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com