请教大家一个PHP获取网页内容的问题!
连接:https://ipinfo.io/8.8.8.8
在浏览器中打开这个网页和在php中打开这个网页,获取的结果是不一样的。
在php中,我使用 file_get_contents 和 curl 获取到的是一个json接口文件。
{ "ip": "8.8.8.8", "hostname": "dns.google", "anycast": true, "city": "Mountain View", "region": "California", "country": "US", "loc": "37.4056,-122.0775", "org": "AS15169 Google LLC", "postal": "94043", "timezone": "America/Los_Angeles", "readme": "https://ipinfo.io/missingauth" }
我的就是一个简单的curl,加了heade和refefer也没有效果。有没有办法让它返回网页源文件呢?谢谢大家了。
function curl_html($url) {
// 创建一个 cURL 句柄
$ch = curl_init();
// 设置 cURL 选项,包括 URL
curl_setopt($ch, CURLOPT_URL, $url);
// 设置 cURL 选项,将结果保存到变量而不是输出到浏览器
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 执行 cURL 请求并获取返回的内容
$output = curl_exec($ch);
// 关闭 cURL 句柄
curl_close($ch);
// 输出获取的内容
return $output;
} 什么源文件,user-agent 了解一下,,, iks 发表于 2024-4-28 13:32
什么源文件,user-agent 了解一下,,,
谢谢您的回复,我加了user-agent也同样返回这个json curl 'https://ipinfo.io/8.8.8.8' \
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36' ywjdlq 发表于 2024-4-28 13:37
curl 'https://ipinfo.io/8.8.8.8' \
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.3 ...
谢谢您的回复,我在cmd下执行错误
C:\Users\Administrator> curl --user-agent "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" https://ipinfo.io/8.8.8.8
Invoke-WebRequest : 找不到接受实际参数“Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1
23.0.0.0 Safari/537.36”的位置形式参数。
所在位置 行:1 字符: 1
+ curl --user-agent "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) ,ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand Telegramr 发表于 2024-4-28 13:43
谢谢您的回复,我在cmd下执行错误
大哥你不会连在linux下执行命令都不会吧 ywjdlq 发表于 2024-4-28 13:37
curl 'https://ipinfo.io/8.8.8.8' \
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.3 ...
我这边返回的也是json ywjdlq 发表于 2024-4-28 13:37
curl 'https://ipinfo.io/8.8.8.8' \
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.3 ...
谢谢您了,我扔到虚拟机里执行成功了,我再检查一下我的代码。 larry 发表于 2024-4-28 13:48
大哥你不会连在linux下执行命令都不会吧
谢谢您了,我扔到虚拟机里执行成功了,我再检查一下我的代码。 怎么都开始学编程了?
通常,结果不一样,不是ua就是referer
页:
[1]
2