php - 尝试从 url 获取 rss 提要时出现一些错误,但在 localhost 上一切正常
问题描述
感谢您抽出宝贵时间帮助我解决问题。好吧,我的问题是我正在尝试获取新闻 rss 提要,所以我构建了我的脚本(我是 rss 提要的业余爱好者,而不是初学者)所以当我运行脚本以将 rss 提要保存到我的本地主机上的数据库时笔记本电脑一切正常,错误中的链接正常工作并且可以访问,但是当我将它上传到我的在线主机时,它给了我这个错误:
警告:simplexml_load_file(https://rss.nytimes.com/services/xml/rss/nyt/World.xml):无法打开流:HTTP 请求失败!HTTP/1.1 403 Forbidden in /home/vol15_7/byethost7.com/b7_27905999/htdocs/cron/news_cron_1.php 在第 24 行
警告:simplexml_load_file():I/O 警告:无法在 /home/vol15_7/byethost7.com/b7_27905999 中加载外部实体“https://rss.nytimes.com/services/xml/rss/nyt/World.xml” /htdocs/cron/news_cron_1.php 第 24 行操作失败注意:尝试访问第 42 行 /home/vol15_7/byethost7.com/b7_27905999/htdocs/cron/news_cron_1.php 中 null 类型值的数组偏移量
我尝试了许多通过谷歌搜索在网上找到的解决方案,但没有一个有效,所以我决定为此创建一个新帖子,我希望它能给社区带来一些东西并帮助遇到此类问题的其他人。
这是我的 rss 捕手和插入数据库的代码:
$sqlCommand = "SELECT * FROM news_feeds_list WHERE cron_file = '1' ORDER BY cron_timestamp ASC";
$query = mysqli_query($connection, $sqlCommand);
while ($row = mysqli_fetch_array($query)) {
$feed_id = $row["feed_id"];
$feed_title = html_entity_decode($row["feed_title"]);
$feed_name = $row["feed_name"];
$feed_url = $row["feed_url"];
$feed_region = $row["feed_region"];
$feed_length = $row["feed_length"];
$feed_notes = $row["feed_notes"];
$feed_subject = $row["feed_subject"];
//libxml_get_errors();
// Here we insert the news into database
$url = "$feed_url";
$xml = simplexml_load_file($url);
if($xml === false){
echo "Operation failed";
continue;
} else {
for($i = 0; $i < $feed_length; $i++) {
$title = $xml->channel->item[$i]->title;
$description = $xml->channel->item[$i]->description;
$link = $xml->channel->item[$i]->link;
$pubDate = $xml->channel->item[$i]->pubDate;
//$author = $xml->channel->item[$i]->author;
$author = '';
$title1 = str_replace("'", "\'", $title);
$description1 = strip_tags($description, '<p><b>');
$description1 = str_replace("'", "\'", $description1);
$author1 = str_replace("'", "\'", $author);
$feed_source_image = $xml->channel->item[$i]->image['url'][0];
$feed_image = $xml->channel->item[$i]->enclosure['url'][0];
$news_timestamp = time();
if($feed_image == ""){
$feed_image = "";
}
// Here we convert pubDate which is a string to a timestamp
$time = strtotime($pubDate);
if($feed_subject == "Politics") {
// IGNORE in the sql query is very important to not insert a duplicate, so it will insert a story once only
$query1 = "INSERT IGNORE INTO news_feeds
(feeds_id, pubDate, news_title, description,
link, author, location, feed_name, feed_subject,
news_timestamp, feed_source_image,
news_image_link)
VALUES($feed_id, '$time', '$title1', '$description1',
'$link', '$author1', '$feed_region',
'$feed_name', 'Politics', $news_timestamp,
'$feed_source_image', '$feed_image')";
$result1 = mysqli_query($connection, $query1);
} else if($feed_subject == "Science") {
// IGNORE in the sql query is very important to not insert a duplicate, so it will insert a story once only
$query1 = "INSERT IGNORE INTO news_feeds
(feeds_id, pubDate, news_title, description,
link, author, location, feed_name,
feed_subject, news_timestamp,
feed_source_image, news_image_link)
VALUES($feed_id, '$time', '$title1',
'$description1', '$link', '$author1',
'$feed_region', '$feed_name', 'Science',
$news_timestamp, '$feed_source_image',
'$feed_image')";
$result1 = mysqli_query($connection, $query1);
} else if($feed_subject == "Technology") {
// IGNORE in the sql query is very important to not insert a duplicate, so it will insert a story once only
$query1 = "INSERT IGNORE INTO news_feeds (feeds_id, pubDate, news_title, description, link, author, location, feed_name, feed_subject, news_timestamp, feed_source_image, news_image_link) VALUES($feed_id, '$time', '$title1', '$description1', '$link', '$author1', '$feed_region', '$feed_name', 'Technology', $news_timestamp, '$feed_source_image', '$feed_image')";
$result1 = mysqli_query($connection, $query1);
} else if($feed_subject == "World News") {
// IGNORE in the sql query is very important to not insert a duplicate, so it will insert a story once only
$query1 = "INSERT IGNORE INTO news_feeds (feeds_id, pubDate, news_title, description, link, author, location, feed_name, feed_subject, news_timestamp, feed_source_image, news_image_link) VALUES($feed_id, '$time', '$title1', '$description1', '$link', '$author1', '$feed_region', '$feed_name', 'World News', $news_timestamp, '$feed_source_image', '$feed_image')";
$result1 = mysqli_query($connection, $query1);
} else if($feed_subject == "Sports") {
// IGNORE in the sql query is very important to not insert a duplicate, so it will insert a story once only
$result1 = mysqli_query($connection, $query1);
}
}
}
}
mysqli_free_result($query);
解决方案
推荐阅读
- go - Go 中是否有 Python 的 `dir()` 和 `help()` 之类的实用程序?
- pandas - Pandas read_csv 将“”附加到某些列
- spring - Spring无法注册用户
- java - 匕首问题 - 生成用例有错误
- python - 在 Pandas 中从 read_excel 设置 Python 变量
- mysql - SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dbs1056212.User_Logs' doesn't exist
- apache-spark - 使用组查询时,Greenplum-spark 连接器无法保存 greenplum 表
- stream - 不推荐使用 AVStream.codec 将编解码器参数传递给复用器,请改用 AVStream.codecpar
- android - 如何通过串口将安卓模拟器与桌面连接?
- c++ - 无法获取 Windows 中正在运行的进程的名称