簡體   English   中英

PHP Curl 無法加載頁面

[英]PHP Curl can't load page

我正在嘗試打開特定頁面( https://www.yellowpages.com.au ) 我嘗試過 simplehtmldom 我也嘗試過 Curl 我嘗試過使用不同的標題並添加了一個證書 我可以打開其他頁面,只是不是這個頁面並想知道該網站如何阻止我的訪問以及我可以做些什么。

$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;

謝謝

網址https://www.yellowpages.com.au不提供頁面,它只是將您(通過HTTP 位置重定向重定向到另一個網址(具體來說: 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 選項進行設置,這是一個工作示例:

<?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="/">

(封頂)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM