This是我的XML格式.ATOM Feed。我收到以下警告。
我在尝试从ATOM URL中提取数据并将其显示为PHP代码并将其保存到DATABASE时遇到问题。我一直收到这些错误......
我尝试过使用loadfile
,curl
,vardump()
,但没有任何效果。
警告: 使用simplexml_load_file(https://colourpop.com/collections/all.atom): 无法打开流:HTTP请求失败! HTTP / 1.1 403禁止进入 第226行/public_html/ecomcharts/beta/indexproducts.php
警告:simplexml_load_file():I / O警告:无法加载外部 实体“https://colourpop.com/collections/all.atom”in /public_html/ecomcharts/beta/indexproducts.php第226行bool(false)
警告:SimpleXMLElement :: __ construct():实体:第1行:解析器错误 :期望开始标记,'<'找不到 第228行/public_html/ecomcharts/beta/indexproducts.php
警告:SimpleXMLElement :: __ construct(): https://colourpop.com/collections/all.atom in 在第228行//ecomcharts/beta/indexproducts.php
我不确定是什么问题。这是我的PHP代码:
<?php
$url = 'https://colourpop.com/collections/all.atom';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
function download_page($path) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
$sXML = download_page('https://colourpop.com/collections/all.atom');
echo $sXML;
$xml=var_dump(simplexml_load_file('https://colourpop.com/collections/all.atom'));
$oXML = new SimpleXMLElement('https://colourpop.com/collections/all.atom');
foreach($oXML->entry as $oEntry) {
echo $oEntry->title . "\n";
}
?>
答案 0 :(得分:0)
使用构造函数创建SimpleXMLElement时,您传入文件的内容而不是文件名,因此在您的情况下......
<?php
$url = 'https://colourpop.com/collections/all.atom';
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$sXML = curl_exec( $ch );
curl_close( $ch );
$oXML = new SimpleXMLElement ( $sXML );
foreach ( $oXML->entry as $oEntry ) {
echo $oEntry->title . "\n";
}
这也解决了尝试使用curl读取HTTPS页面的问题(使用CURLOPT_SSL_VERIFIER方法)