簡體   English   中英

(PHP)使用Curl獲取空白頁(Mytischtennis)

[英](PHP) Getting Blank Page with Curl (Mytischtennis)

我是php的新手,正在嘗試編寫一些東西。

第一步,我想顯示以下頁面:我的網站上的“ https://www.mytischtennis.de/public/home ”。 我正在使用Curl來抓取頁面。 但是每次我要輸出頁面時,我都會得到一個空白頁面。

我的代碼如下所示:

<?php

function grab_page($site){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_URL, $site);

    ob_start();
    return curl_exec($ch);
    ob_end_clean();
    curl_close($ch);
}

echo grab_page("https://www.mytischtennis.de/public/home");
echo "hallo";
?>    

在其他頁面上,此代碼有效。 僅用於“ https://www.mytischtennis.de/public/home ”,它對我有用嗎? 有人可以幫我為什么我只能在該網站上獲得空白頁嗎?

謝謝 :)

您需要在curl請求中設置另外兩個選項:

// Add some more headers here if you need to
$headers = [
    "Accept-Encoding: gzip, deflate, br"
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Decode the response automatically when needed
curl_setopt($ch, CURLOPT_ENCODING, '');

之后,您將獲得所需的頁面。

暫無
暫無

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

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