duanjian3338 2017-12-06 16:11
浏览 82
已采纳

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

I´m new to php and trying to program a few things.

In the first step I want to show the following page: "https://www.mytischtennis.de/public/home" on my website. I am using Curl to grab the page. But every time I want to output the page I am getting a blank page.

My code looks like this:

<?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";
?>    

With other pages this code works. Only for "https://www.mytischtennis.de/public/home" it wont work for me? Can someone help me why i get only with this site a blank page?

Thank you :)

  • 写回答

1条回答 默认 最新

  • doukao8851 2017-12-06 18:38
    关注

    You need to set two more options in your curl request:

    // 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, '');
    

    After this you will get the page you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?