file_get_contents 无法打开流但 curl 给出 401 错误

file_get_contents fails to open stream but curl gives 401 error

我正在尝试使用 file_get_contents 抓取和 svg 并将其放在页面上。在客户端服务器上,它给出了错误 "failed to open stream"。我发现解决此问题的一些建议是使用 curl,但是当我这样做时,我收到了错误 401 unauthorized access。

有什么想法可以解决这两种方法中的一种吗?

function curl_get_contents($url){
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

echo curl_get_contents('http://example.com/path/to/logo.svg'); 

我想通了这个问题。该站点受密码保护,您需要将其他参数传递给 file_get_contents 才能以这种方式访问​​它。这不是我以前 运行 接触过的东西。