使用 php 从给定 URL 下载文件,通过传递用户名和密码进行 http 身份验证

发布于 2024-08-30 20:21:45 字数 451 浏览 8 评论 0原文

我需要使用 php 代码下载文本文件。该文件具有 http 身份验证。我应该为此使用什么程序。我应该使用fsocketopen还是curl或者还有其他方法可以做到这一点吗?

我正在使用 fsocketopen 但它似乎不起作用。

$fp=fsockopen("www.example.com",80,$errno,$errorstr);

$out = "GET abcdata/feed.txt HTTP/1.1\r\n";

$out .= "User: xyz \r\n";

$out .= "Password: xyz \r\n\r\n";
fwrite($fp, $out);

while(!feof($fp))
{

 echo fgets($fp,1024);

}

fclose($fp);

这里 fgets 返回 false。

任何帮助!

I need to download a text file using php code. The file is having http authentication. What procedure I should use for this. Should I use fsocketopen or curl or Is there any other way to do this?

I am using fsocketopen but it does not seem to work.

$fp=fsockopen("www.example.com",80,$errno,$errorstr);

$out = "GET abcdata/feed.txt HTTP/1.1\r\n";

$out .= "User: xyz \r\n";

$out .= "Password: xyz \r\n\r\n";
fwrite($fp, $out);

while(!feof($fp))
{

 echo fgets($fp,1024);

}

fclose($fp);

Here fgets is returning false.

Any help!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南笙 2024-09-06 20:21:45

最简单的方法可能是将 http://username:password@host/path/file 与fopen(如果启用了 url 包装器),但如果您不喜欢,我会使用curl。未经测试,但它可能应该类似于:

$out = fopen($localfilename, 'wb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_exec($ch);
curl_close($ch);
fclose($out);

$localfilename 应包含要写入的本地文件, username:password 必须替换为用于基本身份验证的实际用户名和密码,并用列 (:) 分隔。

The easiest way probably will be using http://username:password@host/path/file with fopen (if url wrappers are enabled), but if you don't like that I would use curl. Not tested, but it should probably be something like :

$out = fopen($localfilename, 'wb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_exec($ch);
curl_close($ch);
fclose($out);

$localfilename should contain the local file you want to write to, username:password have to be replaced with the actual username and password used for basic authentication, separated by a column (:).

永言不败 2024-09-06 20:21:45

这是使用带有 HTTP 身份验证的 cURL 方法下载文件的最简单方法

<?php
/*** DOWNLOAD FILE FROM CURL ****/

//API KEY AND PASSWORD
$username='user';
$password='pass';
$usernamePassword = $username . ':' . $password;
$headers = array('Authorization: Basic ' . base64_encode($usernamePassword), 'Content-Type: application/json');

//URL
$url= 'https://yoururl.com';

//FILE NAME
$filename = 'my_file.pdf';

//DOWNLOAD PATH
$path = 'downloads/'.$filename;

//FOLDER PATH
$fp = fopen($path, 'w');

//SETTING UP CURL REQUEST
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);

//CONNECTION CLOSE
curl_close($ch);
fclose($fp);


//DOWNLOAD PDF LINK
echo '<div id="download-pdf"> <a href="'.$path.'" download>DOWNLOAD AS PDF</a></div>';

/*** DOWNLOAD FILE FROM CURL ****/
?>

Here is the simplest way to download a file using cURL method with HTTP authentication

<?php
/*** DOWNLOAD FILE FROM CURL ****/

//API KEY AND PASSWORD
$username='user';
$password='pass';
$usernamePassword = $username . ':' . $password;
$headers = array('Authorization: Basic ' . base64_encode($usernamePassword), 'Content-Type: application/json');

//URL
$url= 'https://yoururl.com';

//FILE NAME
$filename = 'my_file.pdf';

//DOWNLOAD PATH
$path = 'downloads/'.$filename;

//FOLDER PATH
$fp = fopen($path, 'w');

//SETTING UP CURL REQUEST
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);

//CONNECTION CLOSE
curl_close($ch);
fclose($fp);


//DOWNLOAD PDF LINK
echo '<div id="download-pdf"> <a href="'.$path.'" download>DOWNLOAD AS PDF</a></div>';

/*** DOWNLOAD FILE FROM CURL ****/
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文