douwaz34842 2015-03-24 23:58
浏览 134
已采纳

cURL无法获取特定网站的内容

I try to get the content of this website with cURL

www.mytischtennis.de/public/

but it gets no body response. With many other websites the code works:

<?php


$output = grabPage(
    "http://www.mytischtennis.de/public/"
  //"http://www.spiegel.de" //this page and many other pages are working
);

if (is_array($output)) {
    var_dump($output);
} else {
    echo $output;
}

function grabPage($url)
{
    $ch = curl_init();
    $cookiePath= dirname(__FILE__) . "\cookie.txt";

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 50);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    curl_setopt($ch, CURLOPT_COOKIE, 'CFID=c7a592d8-5798-4471-9af4-4c4d954d03cd; cfid=c7a592d8-5798-4471-9af4-4c4d954d03cd; MYTT_COOKIESOK=1; CFTOKEN0=; cftoken=0; SRV=74');
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);

    $fpErrors = fopen(dirname(__FILE__) . '\errorlog.txt', 'w');

    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_STDERR, $fpErrors);

    curl_setopt($ch, CURLOPT_URL, $url);
    ob_start();
    $curl_exec = curl_exec($ch);
    ob_end_clean();


    if ($curl_exec === false) {
        echo 'Error: ' . curl_error($ch);
    } else {
        echo 'Success';
    }

    var_dump(curl_getinfo($ch));
    curl_close($ch);

    return $curl_exec;
}

I tried to read a fiddler/wireshark dump of a browser request to this website. But I can't figure out which of that many requests and which parameters are necessary to get the content. You can test cURL with the url www.mytischtennis.de/public/ also on this website: http://onlinecurl.com/

  • 写回答

1条回答 默认 最新

  • douqiao4450 2015-03-25 00:31
    关注

    You need to accept gzip encoding in the response by sending the appropriate HTTP header in the request:

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip'));
    

    Now your answer from the server might or might not be gziped. The proper way to check that is to interpret the Content-Encoding HTTP header in the response. But you can also do it quick and dirty like this:

    $content = @gzdecode($curl_exec);
    return $content !== false ? $content : $curl_exec;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值