doushao1087 2015-06-27 12:10
浏览 114
已采纳

如何访问ssl或https类型域的xml文件并使用php存储在我们的服务器中

I want to access an XML file stored on another domain. The domain has an SSL certificate. I want to store it in my server folder. How can I achieve this in PHP? I am able to download without HTTPS from other sites, but unable to do it with HTTPS.

<?PHP
    $dom = new DOMDocument();
    $dom->load('www.123.com/abc.xml');
    $dom->save('filename.xml');
?>
  • 写回答

1条回答 默认 最新

  • douwei1128 2015-06-27 17:58
    关注

    We are going to use Curl to download this file. There are 2 option.

    1. The quick fix
    2. The proper fix

    The quick fix, first.

    Warning: this can introduce security issues that SSL is designed to protect against.

    set:

    1. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); to false

    <?PHP
    
    $url = "https://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml";
    
    // create curl resource 
    $ch = curl_init();
    
    //Now set some options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    // Download the given URL, and return output
    $xml = curl_exec($ch);
    
    // Check for errors
    if (curl_errno($ch)) {
        echo 'Curl error: ' . curl_error($ch);
    }
    
    // Close the cURL resource, and free system resources
    curl_close($ch);
    
    //  Write the string $xml to a file, data.xml
    if (file_put_contents ('data.xml', $xml) !== false) {
         echo 'Success!';
    } else {
         echo 'Failed';
    }
    ?>
    

    The second, and proper fix. Set 3 options:

    1. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    2. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    3. curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\CAcert.crt');

    The last thing you need to do is download the CA certificate.


    <?PHP
    
    $url = "https://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml";
    
    // create curl resource 
    $ch = curl_init();
    
    //Now set some options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\CAcert.crt');
    
    
    // Download the given URL, and return output
    $xml = curl_exec($ch);
    
    // Check for errors
    if (curl_errno($ch)) {
        echo 'Curl error: ' . curl_error($ch);
    }
    
    // Close the cURL resource, and free system resources
    curl_close($ch);
    
    //  Write the string $xml to a file, data.xml
    if (file_put_contents ('data.xml', $xml) !== false) {
         echo 'Success!';
    } else {
         echo 'Failed';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘