PHP:无法使用curl或file_get_contents检索正确的ajax响应

时间:2011-12-07 17:50:21

标签: php ajax curl http-headers file-get-contents

  1. Script output。 (碎)
  2. Start page
  3. Frame page
  4. End page
  5. 我无法弄清楚我错过了什么。我试过模仿firefox请求标头,但它不起作用。 此外,框架页面使用javascript ajax请求到达结束页面。它会将数据发布到$post_to_link(请参阅下面的代码),然后导航到megaupload链接所在的expected result(不是我的current result)。

    预期输出:/membersonly/components/com_iceplayer/GMorBMlet.php?url=http%3A%2F%2Fwww.megaupload.com%2F%3Fd%3DVNICBFWL&

    当前输出:

    • file_get_contents输出3
    • curl输出错误403禁止访问

    这是我的代码:

        // call it like so...
        echo GetHosterLink( 1148, 252636, '', '37fn8Oklq', 15, -75 );
        // $s is incremented every second you are 'visiting' the referer page
        // $m decreases below zero when you move your mouse `down` on the start page
    
        function GetHosterLink( $id, $link_id, $cap, $sec, $s, $m )
        {
    
            $link_page = str_replace( '[ID]', $id, 'http://www.icefilms.info/membersonly/components/com_iceplayer/video.php?vid=[ID]' );
            $post = "id={$link_id}&s={$s}&iqs=&url=&m={$m}&cap=&sec={$sec}&t={$id}";
    
            $header = implode( "\r\n", array(
                "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                "Accept-Encoding: gzip, deflate",
                "Accept-Language: en-us,en;q=0.5",
                "Cache-Control: no-cache",
                "Connection: keep-alive",
                "Content-Length: " . strlen( $post ),
                "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
                "Host: www.icefilms.info",
                "Pragma: no-cache",
                "Referer: http://www.icefilms.info/membersonly/components/com_iceplayer/video.php?h=374&w=631&vid={$id}&img=",
                "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0"
            ));
    
            $opts = array('http' =>
                array(
                    'method'  => 'POST',
                    'header'  => implode("\r\n",
                        array(
                            'Content-type: application/x-www-form-urlencoded',
                            'Content-length: ' . strlen( $post ),
                            'Referer: ' . $link_page . '&h=374&w=631',
                            'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0',
                            'Host: www.icefilms.info'
                        )
                    ),
                    'content' => http_build_query(
                        array(
                            'id' => $link_id,
                            's' => $s,
                            'sec' => $sec,
                            't' => $id,
                            'm' => $m,
                            'iqs' => '',
                            'url' => '',
                            'cap' => ''
                        )
                    )
                )
            );
    
            $context  = stream_context_create($opts);
    
            $post_to_link = 'http://www.icefilms.info/membersonly/components/com_iceplayer/video.phpAjaxResp.php';
            $get_result = file_get_contents( $post_to_link, false, $context );
    
            $f_result = cURL::DoRequest( $post_to_link, $post, '',
                array( array( CURLOPT_HTTPHEADER, $header ) ) );
    
            $f_r = array(
                'result' => $f_result,
                'get_result' => $get_result,
                'get_opts' => $opts,
                'get_response' => $http_response_header,
                'req_post' => $post,
                'req_href' => $post_to_link,
                'req_header' => $header
            );
    
            return ( $f_r );
    
        }
    

    这是我的curl.php文件:

    class cURL
    {
    
        public static function DoRequest( $url, $post = '',
            $cookie_file = '', $variables = array() )
        {
    
            $curl = curl_init();
            @session_start();
            $cookie = ( 'PHPSESSID=' . session_id() . '; path=/' );
            @session_write_close();
    
            curl_setopt( $curl, CURLOPT_URL, $url );
            curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
            curl_setopt( $curl, CURLOPT_COOKIE, $cookie );
    
            if ( !empty( $cookie_file ) )
            {
    
                curl_setopt( $curl, CURLOPT_COOKIEFILE, $cookie_file );
                curl_setopt( $curl, CURLOPT_COOKIEJAR, $cookie_file );
    
            }
    
            if ( !empty( $post ) )
            {
    
                //curl_setopt( $curl, CURLOPT_POST, 1 );
                curl_setopt( $curl, CURLOPT_POSTFIELDS, $post );
    
            }
    
            foreach ( $variables as $var )
                curl_setopt( $curl, $var[0], $var[1] );
    
            $result = curl_exec( $curl );
            curl_close( $curl );
    
            return ( $result );
    
        }
    
    }
    

1 个答案:

答案 0 :(得分:3)

尝试使用curl访问初始页面($link_page中的那个网址)并确保您指向的文件:

curl_setopt( $curl, CURLOPT_COOKIE, $cookie );

存在且可写。

然后使用相同的curl资源请求$post_to_link网址。

通过访问初始页面,您将获得Cookie,并确保您的下一个请求有一个有效的会话。这也可以保护您在标题中提供的引用。有很多方法可以计算“自动”请求,例如检查cookie以及实际访问“referer”-ed链接的方法很常见。