【问题标题】:How can I call a function in one class from another class? Rollingcurl如何从另一个类调用一个类中的函数?卷曲
【发布时间】:2018-10-29 16:38:14
【问题描述】:

我使用 Rollingcurl 爬取各种页面。

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();
}

简单来说,函数“most_popular”当然可以调用。但我想为某些功能构建并调用我自己的类。

如果函数“most_popular”在我的类中,那么调用它就不是那么容易了:

我的班级:

<?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/>";
                }
            }

        }
    }
}

有人知道这是如何工作的吗?

这不起作用:

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

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

非常感谢您。

【问题讨论】:

  • 问题的标题也必须是英文。
  • 对不起,编辑。谢谢!
  • 为什么第一个代码块中的函数定义被注释掉了?
  • 这只是一个测试。如果函数直接在文件中,我可以调用它。但我想在我自己的班级中外包这些和其他功能。但我不知道如何用rollingcurl调用函数。 Rollingcurl 需要函数的名称。在一个类中,函数必须使用 $tmdb->function_name 来寻址。因此我的问题是,我该如何称呼它?

标签: php function class curl


【解决方案1】:

要将对象方法用作回调,请创建一个包含对象和方法名称的数组。

$rc = new RollingCurl([$tmdb, 'most_popular']);

【讨论】:

  • 哇!这工作没有任何问题。非常感谢你。
  • 如果它解决了问题,你应该接受答案
猜你喜欢
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
相关资源
最近更新 更多