【发布时间】:2019-03-28 02:59:22
【问题描述】:
我正在尝试解析 RSS 提要,但我得到的似乎是一个空的 DOM 文档对象。我当前的代码是:
$xml_url = "https://thehockeywriters.com/category/san-jose-sharks/feed/";
$curl = curl_init();
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_URL, $xml_url );
$xml = curl_exec( $curl );
curl_close( $curl );
//$xml = iconv('UTF-8', 'UTF-8//IGNORE', $xml);
//$xml = utf8_encode($xml);
$document = new DOMDocument;
$document->loadXML( $xml );
if( ini_get('allow_url_fopen') ) {
echo "allow url fopen? Yes";
}
echo "<br />";
var_dump($document);
$items = $document->getElementsByTagName("item");
foreach ($items as $item) {
$title = $item->getElementsByTagName('title');
echo $title;
}
$url = 'https://thehockeywriters.com/category/san-jose-sharks/feed/';
$xml = simplexml_load_file($url);
foreach ($items as $item) {
$title = $item->title;
echo $title;
}
print_r($xml);
echo "<br />";
var_dump($xml);
echo "<br />hello?";
此代码是根据在堆栈溢出时发现的以下示例中给出的答案和建议来解析相同 url 的两次单独尝试:
Example 1
Example 2
我尝试过或查找过的事情:
1. 检查以确保允许allow_url_fopen
2.确保有UTF编码
3. 验证 XML
4. 之前链接的 Stack Overflow 帖子中提供的代码示例
这是我当前的输出,var_dumps 和 echo's
allow url fopen? Yes
object(DOMDocument)#2 (34) { ["doctype"]=> NULL ["implementation"]=> string(22) "(object value omitted)"
["documentElement"]=> NULL ["actualEncoding"]=> NULL ["encoding"]=> NULL
["xmlEncoding"]=> NULL ["standalone"]=> bool(true) ["xmlStandalone"]=> bool(true)
["version"]=> string(3) "1.0" ["xmlVersion"]=> string(3) "1.0"
["strictErrorChecking"]=> bool(true) ["documentURI"]=> NULL ["config"]=> NULL
["formatOutput"]=> bool(false) ["validateOnParse"]=> bool(false) ["resolveExternals"]=> bool(false)
["preserveWhiteSpace"]=> bool(true) ["recover"]=> bool(false) ["substituteEntities"]=> bool(false)
["nodeName"]=> string(9) "#document" ["nodeValue"]=> NULL ["nodeType"]=> int(9) ["parentNode"]=> NULL
["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL
["previousSibling"]=> NULL ["attributes"]=> NULL ["ownerDocument"]=> NULL ["namespaceURI"]=> NULL
["prefix"]=> string(0) "" ["localName"]=> NULL ["baseURI"]=> NULL ["textContent"]=> string(0) "" }
bool(false)
hello?
【问题讨论】:
-
您之前看到的答案都没有使用 SSL。看看stackoverflow.com/questions/4372710/php-curl-https我认为问题是证书。
-
嗯,我尝试了 quickfix
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false);只是想看看它是否有效,但它没有。另外,我想这也是一个安全问题。
标签: php xml domdocument