[英]How to download a pdf file on server from other server php
I am stuck in a situation where I have to download a pdf
file from an absolute path to our server but I am unable to do so. 我陷入困境,我必须从绝对路径下载pdf
文件到我们的服务器,但我无法这样做。 All process working in background and I don't want that front user can see what will happen in background. 所有流程都在后台运行,我不希望前端用户可以看到后台会发生什么。
Can anyone provide any solution? 谁能提供任何解决方案? May be it is so simple but I am stuck. 可能是这么简单,但我被卡住了。
使用一些HTTP客户端库,如PHP CURL
Yes you can use PHP CURL
but it will block until you receive file from the external resource.Not a good option if file is bit heavier. 是的,您可以使用PHP CURL
但它会阻止,直到您从外部资源接收文件。如果文件有点重,不是一个好的选择。 You may want to run your script as a cronjob
in that scenario or use some multi-threaded library like multirequest 您可能希望在该场景中将脚本作为cronjob
运行,或者使用某些多线程库(如multirequest)
Here is bare-bone code snippet to make a http request.I have not tested it but you can get some insight and tweak/debug it yourself. 这是一个生成http请求的裸骨代码片段。我还没有测试过它,但你可以获得一些见解并自己调整/调试它。
function saveCompleteRequestToFile(MultiRequest_Request $request, MultiRequest_Handler $handler) {
$filename = preg_replace('/[^\w\.]/', '', $request->getUrl());
file_put_contents('directory_path_here' . $filename, $request->getContent());
}
$mrHandler = new MultiRequest_Handler();
$mrHandler->onRequestComplete('saveCompleteRequestToFile');
$headers = array();
$headers[] = 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Keep-Alive: 300';
$headers[] = 'Accept-Charset: UTF-8,Windows-1251,ISO-8859-1;q=0.7,*;q=0.7';
$headers[] = 'Accept-Language: ru,en-us,en;q=0.5';
$headers[] = 'Pragma:';
$mrHandler->requestsDefaults()->addHeaders($headers);
$options = array();
$options[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
$mrHandler->requestsDefaults()->addCurlOptions($options);
$request = new MultiRequest_Request('http://somedomain/path/file.pdf');
$mrHandler->pushRequestToQueue($request);
$mrHandler->start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.