簡體   English   中英

PHP-file_get_contents不能獲取所有內容(嘗試使用curl,結果相同)

[英]PHP - file_get_contents doesn't get all contents (try with curl, same result)

我嘗試獲取網頁的源代碼。

$urlArena = 'http://arenavision.in/';
$suffixeSchedule = 'schedule';
$url = $urlArena.$suffixeSchedule;
//url = http://arenavision.in/schedule
$text = file_get_contents($url);
$fp = fopen('data.txt', 'w');
$text .= date('d-m-Y h:m:s'); 
fwrite($fp, $text);
fclose($fp);

我將其寫在文件上以確保包含var $ text的內容:

<html>
   <head>
        <script type="text/javascript">
            <pre>
              //<![CDATA[
              try{if (!window.CloudFlare) {var CloudFlare=         
           [{verbose:0,p:0,byc:0,owlid:"cf",bag2:1,mirage2:0,oracle:0,paths:{cloudflare:"/cdn-cgi/nexp/dok3v=1613a3a185/"},atok:"aea30972f99dcd729c29d94acbb3cc58",petok:"87f9b51be2424b953e36dd5ec0f8ce1b0f74a3b5-1493799639-1800",zone:"arenavision.in",rocket:"a",apps:{}}];document.write('<script type="text/javascript" src="//ajax.cloudflare.com/cdn-cgi/nexp/dok3v=85b614c0f6/cloudflare.min.js"><'+'\/script>');}}catch(e){};
//]]>
        </script>
        <script type="text/rocketscript">function set_cookie(){var now = new 
            Date();var time = now.getTime();time += 19360000 * 
            1000;now.setTime(time);document.cookie='beget=begetok'+'; 
            expires='+now.toGMTString()+'; path=/';}set_cookie();location.reload();;
        </script>
    </head>
    <body></body>
</html>
03-05-2017 08:05:46
</pre>

網頁上是否有腳本取消了file_get_contents函數? 我可以避免嗎?

我試着卷曲,但我得到相同的結果。 我嘗試使用另一個網站(google.com),可以獲取所有源代碼。

預先感謝您的幫助,

G。

該網站上的內容是動態生成的。 因此,您無法下載瀏覽器中可以看到的完整頁面。

在此處輸入圖片說明

無論如何,站點受某些雲系統保護。 但是您可以在請求中提供cookie以獲得完整頁面: 在此處輸入圖片說明

在此處輸入圖片說明

您必須模擬真實用戶-在請求中添加cookie,在第一個響應中接受它們。 使用CURL實現

該網站需要一些cookie才能獲取所需的頁面。

這是場景:

1)卷曲第一頁http://arenavision.in

2)使用正則表達式獲取此值

document.cookie='beget=begetok'
//               ^^^^^^^^^^^^^

3)發送該cookie值到下一個請求。


這是使用cURL終端命令的快速示例:-

curl 'http://arenavision.in/'

輸出:

<html><head><script>function set_cookie(){var now = new Date();var time = now.getTime();time += 19360000 * 1000;now.setTime(time);document.cookie='beget=begetok'+'; expires='+now.toGMTString()+'; path=/';}set_cookie();location.reload();;</script></head><body></body></html>

在下一個請求中使用document.cookie的值將達到目的:

curl 'http://arenavision.in/' -H 'Cookie: beget=begetok'

感謝Alex Slipknot和hassan。

您的兩種解釋都對我有所幫助。 這樣就可以了:)
這是我的最終代碼:

$url = $urlArena.$suffixeSchedule;
$text = get_data($url);
$fp = fopen('data.txt', 'w');
$text .= date('d-m-Y h:m:s'); 
fwrite($fp, $text);
fclose($fp);

function get_data($url) {
$cookie = get_cookie($url);
if(!isset($cookie) || strlen($cookie) == 0)
{
    debug('error : '.$cookie.' strlen : '.strlen($cookie));
    return false;
}
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

function get_cookie($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
preg_match('/document.cookie=\'([^\']+)\'/',$data,$m);
print_r($m);
return $m[1];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM