2016-12-18 17 views
0

我想刮簡單的html dom kickasstorrents,但我得到一個錯誤,我甚至還沒有開始。我跟着一些簡單的HTML教程,我已經設置了我的URL並使用curl。試圖抓住簡單的html dom kickasstorrents

代碼如下:

<?php 
require('inc/config.php'); 
include_once('inc/simple_html_dom.php'); 

function scrap_kat() { 

// initialize curl 
$html = 'http://katcr.to/new/'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $html); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
$ip=rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip")); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/".rand(3,5).".".rand(0,3)." (Windows NT ".rand(3,5).".".rand(0,2)."; rv:2.0.1) Gecko/20100101 Firefox/".rand(3,5).".0.1"); 
$html2 = curl_exec($ch); 
if($html2 === false) 
{ 
    echo 'Curl error: ' . curl_error($ch); 
} 
else 
{ 
    // create HTML DOM 
    $kat = file_get_contents($html); 
} 
curl_close($ch); 

// scripting starts 




// clean up memory 
$kat->clear(); 
unset($kat); 
// return information 
return $ret; 

} 
$ret = scrap_kat(); 
echo $ret; 
?> 

我收到錯誤

Fatal error: Call to a member function clear() on resource in C:\wamp64\www\index.php on line 36

我該怎麼辦錯了嗎? 謝謝。

+0

我可以確認那個頁面上的simple-html-d扼流圈。你可以[嘗試作爲替代](https://github.com/monkeysuffrage/advanced_html_dom)。 – pguardiario

回答

0

Simple_html_dom是一類。在那個類中可能有一個函數調用,清除或者它在Simple_html_dom_node類中。但是在簡單的html dom中,你需要使用simple_html_dom類。

@Hassaan,是對的。 file_get_contents是一個本地php函數,你必須創建一個simple_html_dom類的對象。像,

$html = new simple_html_dom(); 

並使用下面的代碼。

function scrap_kat() { 
$url = 'http://katcr.to/new/'; 
// $timeout= 120; 
# create object 
$html = new simple_html_dom(); 
#### CURL BLOCK #### 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/".rand(3,5).".".rand(0,3)." (Windows NT ".rand(3,5).".".rand(0,2)."; rv:2.0.1) Gecko/20100101 Firefox/".rand(3,5).".0.1"); 
//curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); 
$ip=rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip")); 
$content = curl_exec($curl); 
curl_close($curl); 
# note the variable change. 
# load the curl string into the object. 
$html->load($content); 
//echo $ip; 
#### END CURL BLOCK #### 
print_r($html->find('a')); 
// clean up memory 
$html->clear(); 
unset($html); 
} 
scrap_kat(); 

嗯,他們在你的代碼中有很多錯誤,所以我只是告訴你如何做到這一點。如果需要解釋,請在此答案下方評論。我會。

0

file_get_contents是PHP的內置函數。對於簡單的HTML DOM可以使用file_get_html

更換

$kat = file_get_contents($html); 

$kat = file_get_html($html); 

爲什麼要返回$ret;在你的問題你的代碼。沒有可變$ret在你發揮作用scrap_kat()

您可以返回$kat,而不是$ret和不unset($kat);

+0

我試過了不同的方法。 –

+0

我已經嘗試了不同的方法,如果你去:http://pastebin.com/CD8M9eiF,看看...現在我得到:C:\ wamp64 \ www \ index.php:40:null做var_dump時,所以沒有什麼可以通過...任何想法? –