近来在runoob网学习php 及配套的 css等知识
时常写一小段代码测试并分析一下功能等
在看到php fopen函数时 发现 拿runoob试水的时候 返回的竟然是乱码
[PHP] 纯文本查看 复制代码 <?php
//$url = 'https://www.runoob.com/cssref/css3-pr-animation-timing-function.html'; // 你想要请求的 URL
$refurl = 'https://www.runoob.com/cssref/pr-outline-color.html'; // 伪装的 Referer URL
// 目标URL
$url = 'https://www.runoob.com/cssref/css3-pr-animation-timing-function.html';
// 创建一个新的Context选项
$opts = stream_context_create(array(
'http' => array(
//'header' => 'Referer: ' . $refurl, // 设置referrer信息
'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding' => 'gzip, deflate, br',
'accept-language' => 'zh-CN,zh;q=0.9',
'cache-control' => 'no-cache',
'cookie' => 'runoob-uuid=8b300359-ca76-4deb-8243-4a3f677b4e5f',
'pragma' => 'no-cache',
'referer' => 'https://www.runoob.com/cssref/pr-charset-rule.html',
'sec-fetch-dest' => 'document',
'sec-fetch-mode' => 'navigate',
'sec-fetch-site' => 'same-origin',
'sec-fetch-user' => '?1',
'upgrade-insecure-requests' => '1',
'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36)',
)));
// 使用带有Context的fopen函数
$file = fopen($url, 'r', false, $opts);
// 读取数据并输出
$data = fread($file, 500000);
fclose($file);
echo $data;
?>
同样 使用 curl https://www.runoob.com/cssref/css3-pr-animation-timing-function.html时
发现也不正常 不像别的网页一样 返回 网页源码
也在python里面测试 发现是正常
[Python] 纯文本查看 复制代码 import requests
import os
from urllib import parse
from bs4 import BeautifulSoup
url = "https://www.runoob.com/cssref/css-reference.html"
response = requests.get(url)
print(response)
if response.status_code == 200:
html_content = response.text
print(html_content)
else:
print(f"Failed to retrieve the webpage: Status code {response.status_code}")
我想这应该是runoob 在服务器做了限制 那这是如何实现的? 还有 使用php代码的话 怎样才能不至于获取到的是乱码? |