doujiu9307 2018-10-29 16:38
浏览 318
已采纳

如何从另一个类调用一个类中的函数? Rollingcurl

I use Rollingcurl to crawl various pages.

Rollingcurl: https://github.com/LionsAd/rolling-curl

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require "RollingCurl.php";
require "tmdb_class.php";
$tmdb = new Tmdb;

if (isset($_GET['action']) || isset($_POST['action'])) {
    $action = (isset($_GET['action'])) ? $_GET['action'] : $_POST['action'];
} else {
    $action = "";
}

echo "  Test<br /><br />";


/*function most_popular($response, $info)
{

    $doc = new DOMDocument();

    libxml_use_internal_errors(true); //disable libxml errors

    if (!empty($response)) {
        //if any html is actually returned

        $doc->loadHTML($response);
        libxml_clear_errors(); //remove errors for yucky html

        $xpath = new DOMXPath($doc);

        //get all the h2's with an id
        $row   = $xpath->query("//div[contains(@class, 'lister-item-image') and contains(@class, 'float-left')]/a/@href");
        $nexts = $xpath->query("//a[contains(@class, 'lister-page-next') and contains(@class, 'next-page')]");
        $names   = $xpath->query('//img[@class="loadlate"]');

        foreach ($nexts as $next) {
            echo "Next URL: " . $next->getAttribute('href') . "<br/>";
        }

        foreach ($names as $name) {
            echo "Release Name: " . $name->getAttribute('alt') . "<br/>";
        }

        if ($row->length > 0) {
            foreach ($row as $row) {
                echo $doc->saveHtml($row) . "<br/>";
            }
        }

    }
}*/

if ($action == "most_popular") {

    if (isset($_GET['date'])) {
        $link = "https://www.imdb.com/search/title?title_type=feature,tv_movie&release_date=,".$_GET['date'];
    } else {
        $link = "https://www.imdb.com/search/title?title_type=feature,tv_movie&release_date=,2018";
    }

    $urls            = array($link);
    $rc              = new RollingCurl("most_popular");
    $rc->window_size = 20;
    foreach ($urls as $url) {
        $request = new RollingCurlRequest($url);
        $rc->add($request);
    }
    $stream = $rc->execute();
}

In a simple way, the function "most_popular" can of course be called. But I want to build and call my own class for some functions.

If the function "most_popular" is in my class, it is not so easy to call it:

My Class:

<?php

class Tmdb
{


    public function __construct()
    {
      /* */
    }

    // SEARCH
    public function most_popular($response, $info)
    {

        $doc = new DOMDocument();

        libxml_use_internal_errors(true); //disable libxml errors

        if (!empty($response)) {
            //if any html is actually returned

            $doc->loadHTML($response);
            libxml_clear_errors(); //remove errors for yucky html

            $xpath = new DOMXPath($doc);

            //get all the h2's with an id
            $row   = $xpath->query("//div[contains(@class, 'lister-item-image') and contains(@class, 'float-left')]/a/@href");
            $nexts = $xpath->query("//a[contains(@class, 'lister-page-next') and contains(@class, 'next-page')]");
            $names = $xpath->query('//img[@class="loadlate"]');

            foreach ($nexts as $next) {
                echo "Next URL: " . $next->getAttribute('href') . "<br/>";
            }

            foreach ($names as $name) {
                echo "Release Name: " . $name->getAttribute('alt') . "<br/>";
            }

            if ($row->length > 0) {
                foreach ($row as $row) {
                    echo $doc->saveHtml($row) . "<br/>";
                }
            }

        }
    }
}

Does anyone have any idea how this works?

This isn't working:

$rc              = new RollingCurl($tmdb->most_popular);

or

$rc              = new RollingCurl($tmdb->most_popular());

Thank you very much in advance.

  • 写回答

1条回答 默认 最新

  • drfb52000 2018-10-29 16:43
    关注

    To use an object method as a callback, you make an array containing the object and the method name.

    $rc = new RollingCurl([$tmdb, 'most_popular']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog