如何使用php通过https下载文件 [英] How to download a file over https using php

查看:115
本文介绍了如何使用php通过https下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用PHP下载xml文件。我可以通过在卷曲调用时设置以下选项来读取文件的内容。

I need to download an xml file using PHP. I am able to read the contents of file by setting following options when making a curl call to it.

curl_setopt ($http, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($http, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($http,CURLOPT_USERAGENT,'XYZ');

调用该文件是因为该文件不可公开访问,并且只有特定的应用程序才能调用以读取该文件(网络安全技术和资料)

As evident from above snippet I need to make VERIFYHOST as false and also need to set an explicit USERAGENT when making a call to read the file because the file is not publicly accessible and only a specific application can make call to read it (Web-Security techniques and stuff)

我是否下载了相同的文件,我尝试了 readfile fopen / fpassthru file_get_contents ,但似乎没有任何作用。
Readfile给出错误无法打开流:HTTP请求失败! HTTP / 1.0 403 Forbidden

But how do I download the same file, I have tried readfile, fopen/fpassthru, file_get_contents but nothing seems to be working. Readfile gives the error failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

是否可以通过 fopen 或 readfile ,我认为这可能是解决此问题的最简单方法。我在PHP文档中找不到太多帮助。

Is there a way to provide curl like options with fopen or readfile, I think that may be the easiest approach to solve this. I could not find much help in PHP documentation.

请帮助。

推荐答案

您需要在流上下文中附加特殊的用户代理。

You need to attach your special user-agent to the stream context.

来自 http://www.php.net/manual/zh/wrappers.http.php

使用HTTP GET方法,允许通过HTTP 1.0对文件/资源​​的只读访问。与请求一起发送Host:标头,以处理基于名称的虚拟主机。 如果您已使用php配置了 user_agent字符串.ini文件或流上下文,它也将包含在请求中

Allows read-only access to files/resources via HTTP 1.0, using the HTTP GET method. A Host: header is sent with the request to handle name-based virtual hosts. If you have configured a user_agent string using your php.ini file or the stream context, it will also be included in the request.

Example# 2从上面的链接:

Example #2 from the link above:

<?php
ini_set('user_agent', "PHP\r\nX-MyCustomHeader: Foo");

$fp = fopen('http://www.example.com/index.php', 'r');
?>

这篇关于如何使用php通过https下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆