【发布时间】:2017-06-13 06:14:33
【问题描述】:
我正在为我们客户的一个网站进行网络抓取。一切正常。但是我遇到了一个字体不起作用的问题。我在 chrome 控制台中收到以下错误:
在以下位置访问字体 'https://www.example.com/fonts/fontawesome-webfont.woff?v=4.2.0' 来自 来源“http://www.mydomain”已被 CORS 策略阻止:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用来源“http://www.mydomain” 访问。
我尝试将以下代码放入 http://www.mydomain .htaccess 文件中,但没有成功
.htaccess
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With"
Header set Access-Control-Allow-Methods "GET, PUT, POST"
</FilesMatch>
</IfModule>
注意:我无法对https://www.example.com 进行任何更改,并且我的浏览器缓存也被禁用。
网页抓取的php代码:
$cookie = 'cookies.txt';
$timeout = 90;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_FILETIME, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
编辑
apache 标头模块也已启用
【问题讨论】:
-
请检查是否启用了标头模块或未使用
a2enmod headers。 -
@PareshBarad 抱歉。我不能得到你。你能解释一下吗?
-
我已经检查了你的代码,但我没有发现你的代码有任何问题,所以我给你一点建议来启用 apache headers module,如果你正在使用Linux 系统或服务器然后你可以关注这个answer
-
标头模块已启用@PareshBarad。我在 Windows 上使用 WAMP
-
有人不断对我的所有问题投反对票!!!无需说明任何理由
标签: php .htaccess fonts web-scraping