php - 在移动到下一个项目之前等待 PHP 中的任务完成

标签 php mysql rss readability

这里有一个简单的问题,我有一个正在运行的进程可以获取 RSS 提要并将它们添加到 mySQL 数据库中。

在此过程中,我将使用 Readability API 随时获取 URL 内容。

现在这在单个条目上工作正常,但由于这个脚本可以有数百个条目,所以没有任何内容被插入到我的数据库中。

我想知道它是否没有机会完成该过程并立即跳到 RSS 中的下一个条目。

任何人都可以建议在继续之前让它完成的方法吗?代码如下:

$db_hostname="localhost";
$db_username="myusername";
$db_password="mypassword";

try
{
/*  query the database */

$db = mysql_connect($db_hostname,$db_username,$db_password);
if (!$db)
{
    die("Could not connect: " . mysql_error());
}
mysql_select_db("MyDB", $db);


// Get stories that don't have a the readability assigned
$query="select item_id, item_url from tw_articles_parse where story_readability = '' LIMIT 0 , 1";
$result=mysql_query($query);
$num=mysql_numrows($result);
// Close the DB connection
mysql_close();


// Start the loop of source RSS feeds
$i=0;
while ($i < $num) {

    $item_url=mysql_result($result,$i,"item_url");
    $item_id=mysql_result($result,$i,"item_id");

    // Parse the story URL into the Readability API
        $url = "https://www.readability.com/api/content/v1/parser?url=$item_url&token=myapikey";
        // Get the contents of the JSON returned by the API
        $json = file_get_contents($url);
        // Decode the JSON
        $out = json_decode($json, true);
        // Set the content as a variable
        $story = mysql_real_escape_string($out['content']);

        // Insert into the DB - Adding 0 to story_club_id as default
        $item_insert_sql = "UPDATE tw_articles_parse SET story_readability=$story WHERE item_id='" . $item_id . "'";
        $insert_item = mysql_query($item_insert_sql, $db);



$i++;
}// end the loop of feeds


   } catch (Exception $e)
{
echo 'Caught exception: ',  $e->getMessage(), "\n";
}

最佳答案

可能没有插入任何内容,因为您正在使用 UPDATE 语句,并且根本没有要更新的具有相应 item_id 的记录? 尝试将 UPDATE 查询更改为 INSERT ... ON DUPLICATE KEY UPDATE

很遗憾,我们不知道您的数据库方案,但这样的事情应该可行:

$item_insert_sql = "INSERT INTO tw_articles_parse (story_readability, item_id) VALUES ('$story', $item_id) ON DUPLICATE KEY UPDATE story_readability='$story'";

关于php - 在移动到下一个项目之前等待 PHP 中的任务完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10540702/

相关文章:

php - 如何确定 PHP 和 sendmail 是否协同工作?

php - 将id插入MYSQL表进行多图片上传(民宿)

Javascript 代码无法正常工作并为另一部分代码带来值(value)

php - 带圆角的 imagefilledrectangle 函数

php - 如何将 Solr 搜索与 PHP 集成?

php - 将 RSS pubdate 转换为 mysql 数据库中的时间戳

java - DOM 解析器在纯 HTML RSS 帖子上接收 NullPointerException

xml - 我可以在 JSON 中提供 RSS 服务吗?

mysql - 当关键字包含符号时​​sphinx搜索错误

python - 在 MacOSX10.6 上运行 python 服务器时出现 MySQLdb 错误