php - 如何从另一个类调用一个类中的函数?卷曲
问题描述
我使用 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());
非常感谢您提前。
解决方案
要将对象方法用作回调,请创建一个包含对象和方法名称的数组。
$rc = new RollingCurl([$tmdb, 'most_popular']);
推荐阅读
- ios - 关闭所有呈现的视图控制器 modalPresentationStyle = .custom
- python - Django Rest Framework 中 id 的迁移错误
- android - android自定义对话框标题不显示
- python - 通过 API 调用获取数据,该调用需要使用 Excel VBA 的凭据
- c - 有没有办法让我的 do-while 条件在间隔之间起作用?
- typescript - 在 TypeScript 中使用 AM/PM 的日期之间过滤对象数组
- jquery - 由 Jquery 动态创建的数组的文本字段仅提交第一个条目
- r - 使用 tidyverse、ggplot 和 broom 为 R 中的比例检验 (prop.test) 添加置信区间
- c++ - 具有对数刻度的 QwtPlotSpectrogram
- python - 无法在 pygame 中正确地做抛射物