简体   繁体   English

将计划的XML解析为SQL数据库的最简单方法是什么?

[英]What is the simplest way to make scheduled XML parsing into an SQL database?

So, ok, I'm using PHP for my website, and suppose I know quite a bit of MySQL and a little bit of MS SQL. 好吧,我在网站上使用PHP,并且假设我了解很多MySQL和一些MS SQL。 Now, I want to parse some XML with PHP/USD exchange rate and store it in the database. 现在,我想用PHP / USD汇率解析一些XML并将其存储在数据库中。

The simplest way one could think of would be, perhaps this: 一个人想到的最简单的方法可能是:

$XMLContent=file("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency=PHP");

foreach($XMLContent as $line){
$a=&strip_tags($line);
if (is_numeric($a))
{
    // output the exchange rate
    echo $a;
    // THen I would probably go like this:
    mssql_query('update table set xchange_rate='.(float)floatval($a));
    // break cycle once found;
    break;
}
}

That would perfectly do... if it didn't take 0.8 seconds to run this script, due to external XML GET request. 如果由于外部XML GET请求而无需花费0.8秒来运行此脚本,那将完全可以实现。

So I suppose I should use Windows Scheduler, and make a task to run, let's say every hour to update the records. 因此,我想我应该使用Windows Scheduler,并使任务运行,比如说每小时更新一次记录。 Now the question is, I've no idea what script I would use. 现在的问题是,我不知道该使用什么脚本。 I mean, I can't just simply run browser with a PHP script - that would be rediculous. 我的意思是,我不能只是简单地使用PHP脚本运行浏览器-这太麻烦了。

So, what would be the easiest way to make a script/ application to run it outside PHP, without a need to actually open a browser. 因此,使脚本/应用程序在PHP外部运行而无需实际打开浏览器的最简单方法是什么。

Thanks! 谢谢!

EDIT Good point, right now I'm testing it on Win7, but later it is going to be implemented on Windows Server (2008?). 编辑好点,现在我正在Win7上对其进行测试,但是稍后它将在Windows Server(2008?)上实现。

php has a binary executable interpretor too. php也具有二进制可执行解释器。 On win, its called php.exe 获胜时,其名为php.exe

http://php.net/manual/en/features.commandline.php http://php.net/manual/zh/features.commandline.php

get it working first from a shell prompt, then make a scheduled task for it. 首先从shell提示符下使它工作,然后为其安排任务。 keep in mind, it is not the same php as you get when running a script through a webserver. 请记住,它与通过Web服务器运行脚本时得到的PHP不同。 Some settings are different, and the version may be different, and may use a different php.ini file(and so may have different extensions loaded). 某些设置不同,版本可能不同,并且可能使用不同的php.ini文件(因此可能会加载不同的扩展名)。

consider using absolute file paths to start until you get things working. 考虑使用绝对文件路径启动,直到一切正常。

You don't need a browser to run a PHP script. 您不需要浏览器即可运行PHP脚本。 PHP can happily run as a console application. PHP可以愉快地作为控制台应用程序运行。 Without knowing what system you're deploying this on (ie which OS), it's hard to give you more directions. 不知道要在哪个系统上部署它(即哪个OS),很难为您提供更多指导。 For example, on a unix-like system (UN*X, linux, OS X, etc.) you can create your PHP file: 例如,在类似Unix的系统(UN * X,Linux,OS X等)上,您可以创建PHP文件:

somefile.php somefile.php

<?php

.....

?>

save this file into some pre-defined directory, for example, /opt/myproject and then schedule it in /etc/crontab (assuming your php is installed in /usr/bin): 将此文件保存到一些预定义的目录中,例如/opt/myproject ,然后在/ etc / crontab中计划它(假设您的php安装在/ usr / bin中):

7 * * * *    /usr/bin/php /opt/myproject/somefile.php

This would run your script with PHP, without any browser, once an hour, at 7 minutes past each hour. 这将使用每小时7点钟的小时(每小时)在没有任何浏览器的情况下使用PHP运行脚本。

EDIT If you're to deploy this on a windows server, you can either use at command on the command prompt or use the Task Scheduler snap-in in Server Manager to configure your job. 编辑如果要在Windows服务器上部署它,则可以at命令提示符下使用at命令,也可以使用Server Manager的“ Task Scheduler管理单元来配置您的作业。 The PHP script would be saved where you like it to be (eg D:\\Projects\\MyProject\\myfile.php) and the command you would schedule to run would be C:\\WAMP\\bin\\php.exe - or whereve your php.exe is located). PHP脚本将保存在您喜欢的位置(例如D:\\ Projects \\ MyProject \\ myfile.php),您计划运行的命令将是C:\\ WAMP \\ bin \\ php.exe-或您的php位置.exe位于)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM