我使用Curl -dump-header,但是我也拒绝访问。有什么方法可以连接并获取html代码吗?
卷曲--dump-header-https://www.gearbest.com/car-charger/pp_009363232829.html
> HTTP/2 403 server: AkamaiGHost mime-version: 1.0 content-type:
> text/html content-length: 314 cache-control: max-age=60 expires: Sat,
> 22 Jun 2019 22:25:51 GMT date: Sat, 22 Jun 2019 22:24:51 GMT
> set-cookie: AKAM_CLIENTID=7e3530d888ae97fef4ad26c997d733c5;
> expires=Mon, 31-Dec-2038 23:59:59 GMT; path=/; domain=.gearbest.com
> vary: User-Agent
<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>
You don't have permission to access "http://www.gearbest.com/car-charger/pp_009363232829.html" on this server.<P>
Reference #18.85451502.1561242291.25f21039
</BODY>
</HTML>
如果我使用浏览器转到该页面,则效果很好。我只想获取页面的html代码。任何帮助表示赞赏。
答案 0 :(得分:0)
这是您修改过的PHP代码,它运行正常(经过测试):
<?php
$curl1 = curl_init();
$url = "https://gearbest.com/car-charger/pp_009363232829.html";
curl_setopt($curl1, CURLOPT_URL, $url);
curl_setopt($curl1, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($curl1, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0');
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_FOLLOWLOCATION, true);
$str = curl_exec($curl1);
echo $str;
一些注意事项:
curl_exec()
返回HTML代码,但是没有先设置CURLOPT_RETURNTRANSFER
选项就不会发生www.
),要正确处理此问题,您需要设置CURLOPT_FOLLOWLOCATION
选项