月度归档: 2016 年 11 月

  • PHP 解决curl_exec直接显示内容的问题

    PHP 解决curl_exec直接显示内容的问题。

    调用curl_exec会直接显示接收到的内空,而不是保存在变量中,这个问题可以通过设置:

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    来解决

    function getRemoteData() {
        //echo 'from remote';
        $url = 'http://www.6166.cc/game/lotteryServlet?type=showBet&isNewLottery=true&lotteryId=9' ;
        $ch = curl_init($url );
        curl_setopt($ch,CURLOPT_HTTPHEADER,array('Host:www.6166.cc','Upgrade-Insecure-Requests:1'));
        curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        curl_setopt($ch,CURLOPT_COOKIESESSION,true);
        curl_setopt ($ch, CURLOPT_TIMEOUT,500 );
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // 获取302跳转之后的内容
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // 如果不加此句,则curl_exec会直接显示内容,而不是把内容保存到返回值中
        $ret = curl_exec($ch);
    
        curl_close($ch);   
        return $ret;
    }