what would be the proper way to automate an xml import

北战南征 提交于 2020-01-05 15:12:46

问题


I've written a script that imports data from an xml file into the mysql database by selecting it from the source disk and uploading it via a button submital. But what if a 3rd party application were to be used to automate this import. Would it be proper to check if a get parameter of a xml path exist and grab its content and import the same way i did before? or is there a better method?

by get parameter i mean like this:

http://domain.com/import.php?path=externaldomain.com/xml/page.xml

回答1:


it depends on what kind of data you are importing. If you import data from an rss feed, this method is fine. But if you are going to import personal data this might not really be a good method.

I would suggest something more secure if you are working with critical data that others are not supposed to see. You can start thinking of importing the xml files through ftp, download them from behind a server secured folder. Ask the 3rd party application to upload the xml files to a secure location of your choosing. Anything that goes on behind some kind of security is better then the suggested method for personal data.




回答2:


Firstly I'd advice you using cURL. Doesn't matter how huge is your XML will be, you'll have less problems with memory.

$fp = fopen('/var/www/vhosts/my.com/xml/feed.xml', 'w'); // opening file handler to write feed in

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://domain.com/xml/page.xml'); // setting URL to take XML from
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); // If result is gziped
curl_setopt($ch, CURLOPT_SSLVERSION, 3); // OpenSSL issue
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  // Wildcard certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // disabling buffer output, bec. we want to write XML to the file first and don't need it to be returned into variable
curl_setopt($ch, CURLOPT_FILE, $fp); // here we should transfer opened file handler to the cURL and it should be writable!
$result = curl_exec($ch); // executing download
$reponse_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); // retrieving HTTP return code for our request. Was it successful or not.

Thus, you can download/save your XML feed even if it is behind SSL and GZIPed, directly to the file.

Using curl_getinfo() you can get diverse information about your request. If procedure supposed to be automated than it would be nice to decide what to do if request fails.

Than, if file is not large (I mean really large files above 200 - 300 Mb) you can just use SimpleXML (available only since PHP5) library and parse your data. If you are under PHP4 (it is still possible today) try to find libXML which is very useful too.

If file you retrieved is rather huge :) MySQL database with FILE permissions is your friend.



来源:https://stackoverflow.com/questions/11635903/what-would-be-the-proper-way-to-automate-an-xml-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!