简体   繁体   English

使用php从https下载xml文件

[英]Download xml file from https using php

I can download xml file when I click on https://www.omniva.ee/locations.xml . 当我单击https://www.omniva.ee/locations.xml时,我可以下载xml文件。

Is it possible to get the contents of this file using PHP and save these to a MySQL database? 是否可以使用PHP获取此文件的内容并将其保存到MySQL数据库?

I tried this example but without any result (no arrors found but php.ini file on server has value 0): 我尝试了此示例,但没有任何结果(未找到任何错误,但服务器上的php.ini文件的值为0):

PHP Version 5.6.19 Directive Local value Master value PHP版本5.6.19指令本地值主值
allow_url_fopen 0 0 allow_url_include no value no value allow_url_fopen 0 0 allow_url_include无值无值

$xml = file_get_contents("https://www.omniva.ee/locations.xml");

If allow_url_fopen is disabled you have no possibility to get the file content of the external file with file_get_contents() . 如果禁用allow_url_fopen则无法使用file_get_contents()获取外部文件的文件内容。 Instead of using file_get_contents() you can use curl to get the content of the file: 除了使用file_get_contents()您还可以使用curl获取文件的内容:

<?php
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'https://www.omniva.ee/locations.xml');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    $data = curl_exec($curl);

    //check if the curl_exec was successful.
    if (curl_errno($curl) === 0) {
        //success - file could be downloaded.
        //write the content of $data in database here...
    } else {
        //error - file could not be downloaded.
    }

    //close the curl session.
    curl_close($curl);
?>

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

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