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 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色