【问题标题】:How can I download this file?我怎样才能下载这个文件?
【发布时间】:2012-04-25 09:05:10
【问题描述】:

此代码适用于大多数网站,如 google、youtube、facebook 等,但不适用于 technorati 等一些网站:

<?php
$favicon="http://technorati.com/favicon.ico";
$content = file_get_contents($favicon);
file_put_contents('favicon/icon.ico', $content);  

echo "<img src=\"http://localhost/test/favicon/icon.ico\" />";

?>

//输出:

警告:file_get_contents(http://technorati.com/favicon.ico) [function.file-get-contents]:无法打开流:HTTP 请求 失败的! HTTP/1.1 403 禁止在 /opt/lampp/htdocs/test/simple.php 上 第 3 行

http://localhost/test/favicon/icon.ico

如何下​​载 technorati 的网站图标?

【问题讨论】:

  • 你可以手动完成
  • Alp 的解决方案绝对是代码最少的解决方案。更少的代码 == 更少的麻烦。
  • 显然,我需要用 PHP 来做到这一点 :)

标签: php file-get-contents


【解决方案1】:

看看当你发出请求时会发生什么,例如使用 Fiddler 或 Wireshark。

我的猜测是 Technorati 网络服务器被配置为拒绝自动请求,它可能使用爬虫发送的用户代理检测到。

使用cURL,您可以更改用户代理。

【讨论】:

    【解决方案2】:

    尝试模仿浏览器并将用户代理设置为 technorati.com 喜欢的东西:)

    ini_set('user_agent', 'Name of your bot');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      相关资源
      最近更新 更多