[英]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才能獲取所需的頁面。
這是場景:
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.