简体   繁体   English

如何从Web服务获取文件并将其使用php保存到我们的磁盘上?

[英]how to get files from web services and save them on our disk using php?

I could successfully connect to a web service, it provides a list of files that I need to save them on my disk, I am wondering how can I download and save them on my disk? 我可以成功连接到Web服务,它提供了将它们保存在磁盘上所需的文件列表,我想知道如何下载并将它们保存在磁盘上?

simple response is as following (response is shorten) 简单响应如下(响应缩短)

 <files>
   <file>
       <name>1.jpeg</name>
       <address>c:\photos\1.jpeg</address>
       <url>www.xxx.com\photos\1.jpeg</url>
   </file>
   <file>
       <name>my.pdf</name>
       <address>c:\pdfs\my.pdf</address>
       <url>www.xxx.com\pdfs\my.pdf</url>
  </file>
</files>

I've found the following but not sure how to save the $output 我发现以下内容,但不确定如何保存$ output

 function url_get_contents ($Url) {
     if (!function_exists('curl_init')){ 
          die('CURL is not installed!');
     }
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $Url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $output = curl_exec($ch);
     curl_close($ch);
     return $output;
 }

I used the following but something is wrong, it create the file but its empty 我使用了以下内容,但是出了点问题,它创建了文件,但是它为空

$fp = fopen("lib\1",'x');
fwrite($f, $output);
fclose($fp);

Are the files offered by your web service publicly available and without authentication? 您的Web服务提供的文件是否可以公开获得且未经身份验证? If so, there are a heap of ways to get the files, providing you know their full locations... 如果是这样,有很多方法可以获取文件,只要您知道它们的完整位置即可。

You can use PHP's file_get_contents to grab the content of the file then save it somewhere... 您可以使用PHP的file_get_contents来获取文件的内容,然后将其保存在某处...

$page = file_get_contents('NETWORK_ADDRESS\pdfs\my.pdf');

Or you can use CURL, take a look at the answer to this question , there is a full code example showing you how to download a file using CURL and PHP. 或者,您也可以使用CURL,看看这个问题的答案 ,这里有一个完整的代码示例,向您展示如何使用CURL和PHP下载文件。

Or if your files are local (unlikely) to your PHP installation, you can use PHP's Copy function to move the file to where you want it. 或者,如果您的文件位于PHP安装的本地(不太可能),则可以使用PHP的“复制”功能将文件移动到所需的位置。 Read about copy here. 在此处阅读有关复制的信息。

Once you have the file, lets assume in the $page variable as per my example above, use file_put_contents to save the contents of that file... 有了文件后,按照上面的示例,在$ page变量中假设,使用file_put_contents保存该文件的内容...

$file = 'filename.ext';
file_put_contents($file, $page);

More information on file_put contents here . 有关file_put内容的更多信息,请参见此处

There are a heap of ways to do it, providing the file is accessible. 只要文件可访问,有很多方法可以做到。 Give us some more information and we can suggest a suitable solution. 给我们更多信息,我们可以建议合适的解决方案。

You'll need to know the public url in order to download the files. 您需要知道公共URL才能下载文件。 In other words, how does anyone but the server access these files? 换句话说,除服务器外,其他人如何访问这些文件? What kind of web service is this? 这是哪种Web服务? What protocol are you using? 您正在使用什么协议? If it's publicly accessible, you can use file_get_contents to save the file. 如果可以公开访问,则可以使用file_get_contents保存文件。 Otherwise, curl and some authentication will probably suit your needs. 否则, curl和一些身份验证可能会满足您的需求。

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

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