主机论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 322|回复: 19

问个PHP获取网页源代码的问题

[复制链接]

21

主题

172

帖子

481

积分

中级会员

Rank: 3Rank: 3

积分
481
发表于 2024-4-13 15:55:47 | 显示全部楼层 |阅读模式


https://www.ip138.com/iplookup.php?ip=110.242.68.66&&action=2
https://m.ip138.com/iplookup.php?ip=110.242.68.66&&action=2
https://www.ipshudi.com/110.242.68.66.htm

这三个地址,浏览器中都能返回正确的地址信息,但是在php里,无论是使用 file_get_contents()或是curl都会跳转到 https://ipchaxun.com/ 这个页面,但是跳转后的页面源码中却没有任何信息。

请问应该如何去获取到这个页面的一些信息呢?谢谢大家了
回复

使用道具 举报

29

主题

1049

帖子

3009

积分

论坛元老

Rank: 8Rank: 8

积分
3009
发表于 2024-4-13 18:29:58 | 显示全部楼层
这种工具类,要么买接口,要么下免费库自己解析。
不要想着一个curl就白piao,谁也不傻,早都做限制了
回复

使用道具 举报

13

主题

639

帖子

1855

积分

金牌会员

Rank: 6Rank: 6

积分
1855
发表于 2024-4-13 16:00:59 | 显示全部楼层
访问的时候加ua。不加ua会跳转。curl --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36" https://www.ip138.com/iplookup.php?ip=110.242.68.66&&action=2
回复

使用道具 举报

21

主题

172

帖子

481

积分

中级会员

Rank: 3Rank: 3

积分
481
 楼主| 发表于 2024-4-13 16:08:47 | 显示全部楼层
似毛非毛 发表于 2024-4-13 16:00
访问的时候加ua。不加ua会跳转。curl --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537 ...

好的,谢谢您了
回复

使用道具 举报

21

主题

172

帖子

481

积分

中级会员

Rank: 3Rank: 3

积分
481
 楼主| 发表于 2024-4-13 16:00:00 | 显示全部楼层
似毛非毛 发表于 2024-4-13 16:00
访问的时候加ua。不加ua会跳转。curl --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537 ...
i HTTP/1.1 302 Moved Temporarily Server: nginx Date: Sat, 13 Apr 2024 08:26:28 GMT Content-Type: text/html Content-Length: 138 Connection: keep-alive Location: https://www.ipshudi.com/ x-ws-request-id: 661a41b4_houdxin43_20924-28142
302 Found
nginx


我的提示这个错误
回复

使用道具 举报

21

主题

172

帖子

481

积分

中级会员

Rank: 3Rank: 3

积分
481
 楼主| 发表于 2024-4-13 16:27:40 | 显示全部楼层
似毛非毛 发表于 2024-4-13 16:00
访问的时候加ua。不加ua会跳转。curl --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537 ...

我在linux下按照您的方法使用可以获取到信息,但是在php里用curl却返回302
回复

使用道具 举报

0

主题

5

帖子

20

积分

新手上路

Rank: 1

积分
20
发表于 2024-4-13 16:00:00 | 显示全部楼层
说明你ua头没加对呗。又不把代码发出来,都不知道怎么帮你
回复

使用道具 举报

21

主题

172

帖子

481

积分

中级会员

Rank: 3Rank: 3

积分
481
 楼主| 发表于 2024-4-13 17:22:39 | 显示全部楼层
_leo 发表于 2024-4-13 17:31
说明你ua头没加对呗。又不把代码发出来,都不知道怎么帮你
function curl_ip($url)
{
    $ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36";
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_USERAGENT, $ua);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}


谢谢您的回复,这是我的代码
回复

使用道具 举报

21

主题

172

帖子

481

积分

中级会员

Rank: 3Rank: 3

积分
481
 楼主| 发表于 2024-4-13 16:00:00 | 显示全部楼层
_leo 发表于 2024-4-13 17:31
说明你ua头没加对呗。又不把代码发出来,都不知道怎么帮你
function ip_curlGet($url)
{
    $ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // 输出头
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_FALSESTART, true);
    //模拟常用浏览器的 useragent
    curl_setopt($ch, CURLOPT_USERAGENT, "{$ua}");
    // 不跟随跳转
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;

}


两段都没有效果
回复

使用道具 举报

1

主题

797

帖子

1723

积分

金牌会员

Rank: 6Rank: 6

积分
1723
发表于 2024-4-13 17:31:45 | 显示全部楼层
前两个连接,我的浏览器是直接404。应该是检查了referer。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|主机论坛

GMT+8, 2024-4-30 12:55 , Processed in 0.095246 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表