PHP Curl无法加载页面

时间:2019-03-21 09:37:03

标签: php curl

我正在尝试打开特定页面(https://www.yellowpages.com.au) 我已经尝试过simplehtmldom 我也尝试过卷曲 我尝试使用不同的标题并添加了证书 我可以打开其他页面,而不能打开其他页面,并且想知道该站点如何阻止我的访问以及我可以做些什么。

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");


$certificate = "cacert-2019-01-23.pem";
curl_setopt($ch, CURLOPT_CAINFO, $certificate);
curl_setopt($ch, CURLOPT_CAPATH, $certificate);


$data = curl_exec($ch);
curl_close($ch);
echo $data;

谢谢

1 个答案:

答案 0 :(得分:0)

URL https://www.yellowpages.com.au不为页面提供服务,它只是将您(通过HTTP Location Redirect重定向到实际为页面提供服务的另一个URL(具体来说为Location: http://www.yellowpages.com.au/dataprotection?path=/)。为了使用curl加载该URL,必须告诉curl遵循HTTP Location redirects,这可以通过CURLOPT_FOLLOWLOCATION完成,

除此之外,yellowpages.com.au会阻止缺少User-Agent标头的请求,libcurl没有设置默认的User-Agent标头,您可以使用CURLOPT_USERAGENT option进行设置,这是一个有效的示例:

<?php
$ch=curl_init('https://www.yellowpages.com.au');
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_USERAGENT,'libcurl/'.(curl_version()['version']).' PHP/'.PHP_VERSION);
curl_exec($ch);
curl_close($ch);

输出:

$ php foo4.php

<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>

        <title>Yellow Pages&reg; | Data Protection</title>

    <link rel="shortcut icon" href="/favicon.ico?v=2" />

    <!--[if (lt IE 9)&!(IEMobile)]><script src="/assets/ie/respond.sensis-9575467dfbc008e5b0d486dc4f481624.js" type="text/javascript" ></script><![endif]-->
    <!--[if (lt IE 10)&!(IEMobile)]><script src="/assets/ie/custom-event-ie9.js" type="text/javascript" ></script><![endif]-->
    <!--[if (lt IE 10)&!(IEMobile)]><link rel="stylesheet" href="/assets/ie/gradient-hacks-ie89-12453d23f1fec3d9d46e56cc6e023576.css"/><![endif]-->

        <script src="https://www.google.com/recaptcha/api.js?" async defer></script>
        <meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>

</head>
<body style="border-width: 0;
                background-color: #EDEDED;
                font-size: 85%;
                line-height: 1.3;
                margin: 0;
                font-family: Helvetica, sans-serif;" id="">


        <div style="padding: 10px 15px;
                    height: 70px;
                    min-height: 45px;
                    background-color: #ffce00;
                    background-image: linear-gradient(to right, #ffce00, #fedb55, #ffce00);
                    box-shadow: inset 0px -5px 7px -5px rgba(0, 0, 0, 0.35);">
            <div style="position: relative;
                        max-width: 1240px;
                        margin: 0 auto;">
                <a href="/">

(上限)