2016-08-19 69 views
0

我試圖用舊的(死)torrentz.eu刮板碼報廢torrentz2.eu搜索結果:東西不捲曲工作再殺

當我運行http://localhost/jits/torz/api.php?key=kabali 它顯示我警告和空值。

Notice: Undefined variable: results_urls in /Applications/XAMPP/xamppfiles/htdocs/jits/torz/api.php on line 59 
null 

爲什麼?

有人可以告訴我代碼有什麼問題嗎?

這裏是代碼:

<?php 
    $t= $_GET['key']; 
    // Defining the basic cURL function 
    function curl($url) { 
     // Assigning cURL options to an array 
     $options = Array(
      CURLOPT_RETURNTRANSFER => TRUE, // Setting cURL's option to return the webpage data 
      CURLOPT_FOLLOWLOCATION => TRUE, // Setting cURL to follow 'location' HTTP headers 
      CURLOPT_AUTOREFERER => TRUE, // Automatically set the referer where following 'location' HTTP headers 
      CURLOPT_CONNECTTIMEOUT => 120, // Setting the amount of time (in seconds) before the request times out 
      CURLOPT_TIMEOUT => 120, // Setting the maximum amount of time for cURL to execute queries 
      CURLOPT_MAXREDIRS => 10, // Setting the maximum number of redirections to follow 
      CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0", // Setting the useragent 
      CURLOPT_URL => $url, // Setting cURL's URL option with the $url variable passed into the function 
     ); 

     $ch = curl_init(); // Initialising cURL 
     curl_setopt_array($ch, $options); // Setting cURL's options using the previously assigned array data in $options 
     $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable 
     curl_close($ch); // Closing cURL 
     return $data; // Returning the data from the function 
    } 
?> 

<?php 
    // Defining the basic scraping function 
    function scrape_between($data, $start, $end){ 
     $data = stristr($data, $start); // Stripping all data from before $start 
     $data = substr($data, strlen($start)); // Stripping $start 
     $stop = stripos($data, $end); // Getting the position of the $end of the data to scrape 
     $data = substr($data, 0, $stop); // Stripping all data from after and including the $end of the data to scrape 
     return $data; // Returning the scraped data from the function 
    } 
?> 
<?php 

    $url = "https://torrentz2.eu/search?f=$t"; // Assigning the URL we want to scrape to the variable $url 
    $results_page = curl($url); // Downloading the results page using our curl() funtion 
    //var_dump($results_page); 
    //die(); 
    $results_page = scrape_between($results_page, "<dl><dt>", "<a href=\"http://www.viewme.com/search?q=$t\" title=\"Web search results on ViewMe\">"); // Scraping out only the middle section of the results page that contains our results 
    $separate_results = explode("</dd></dl>", $results_page); // Expploding the results into separate parts into an array 

    // For each separate result, scrape the URL 
    foreach ($separate_results as $separate_result) { 
     if ($separate_result != "") { 
      $results_urls[] = scrape_between($separate_result, "\">", "<b>"); // Scraping the page ID number and appending to the IMDb URL - Adding this URL to our URL array 

     } 

    } 

    //print_r($results_urls); // Printing out our array of URLs we've just scraped  

if($_GET["key"] === null) { 
echo "Keyword Missing "; 
    } else if(isset($_GET["key"])) { 

     echo json_encode($results_urls); 

    } 

     ?> 

舊torrentz.eu刮板碼參考:GIT repo

回答

1

第一件事,你會得到通知 「未定義的變量:results_urls」 因爲$ results_urls定義和直接使用。定義它,然後使用它。

做這樣的事情: - 打印

// $results_urls defined here:- 
    $results_urls = []; 
    // For each separate result, scrape the URL 
    foreach ($separate_results as $separate_result) { 
     if ($separate_result != "") { 
      $results_urls[] = scrape_between($separate_result, "\">", "<b>"); // Scraping the page ID number and appending to the IMDb URL - Adding this URL to our URL array 

     } 

    } 

其次,null,因爲$ results_urls是沒有得到填充,因爲$ separate_results是沒有得到正確填充。它只有一個值是空的。 我進一步調試,發現$ results_page值爲false。所以無論你想在「scrape_between」函數不能按預期工作。修復你的功能。

+0

我的錯,我宣佈了,謝謝@umakant。 但我真正的問題是我不知道如何糾正scrape_between().. 這就是爲什麼我問這裏問題,如果有人可以幫我糾正代碼,以便它可以報廢結果。 –

+1

Jeet,在放棄投票回答之前,告訴我你在哪裏提到你的問題?你的問題在哪裏說你想知道你只需要回答修復「scrape_between()」函數?所以如果你還沒有提到這個問題,那顯然這就是答案。 –