东西不卷曲工作再杀

问题描述:

我试图用旧的(死)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

第一件事,你会得到通知 “未定义的变量: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()”函数?所以如果你还没有提到这个问题,那显然这就是答案。 –