(PHP)使用Curl获取空白页面(Mytischtennis)

时间:2017-12-06 16:11:24

标签: php curl

我是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”它不适合我吗? 有人可以帮助我为什么我只得到这个网站一个空白页?

谢谢:)

1 个答案:

答案 0 :(得分:1)

您需要在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, '');

在此之后,您将获得所需的页面。

相关问题