致命错误:调用未定义的方法 wSpider::fetchPage()

发布于 2024-12-10 22:38:25 字数 1538 浏览 0 评论 0原文

错误是:

Fatal error: Call to undefined method wSpider::fetchPage()

首先,我想做的是构建一个蜘蛛来从网页获取数据。我不太确定为什么会出现这个错误,但我对 php 还很陌生,所以这可能是我遗漏的一些相当明显的东西。代码:

<?php
class wSpider
{
var $ch; /// going to used to hold our cURL instance
var $html; /// used to hold resultant html data
var $binary; /// used for binary transfers
var $url; /// used to hold the url to be downloaded

function wSpider()
{
$this->html = "";
$this->binary = 0;
$this->url = “”;
}
}

function fetchPage($url)
{
$this->url = $url;
if (isset($this->url)) {
$this->ch = curl_init (); /// open a cURL instance
curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, 1); // tell cURL to return the data
curl_setopt ($this->ch, CURLOPT_URL, $this->url); /// set the URL to download
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); /// Follow any redirects
curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, $this->binary); /// tells cURL if the data is binary data or not
$this->html = curl_exec($this->ch); // pulls the webpage from the internet
curl_close ($this->ch); /// closes the connection
}
}

$mySpider = new wSpider(); //// creates a new instance of the wSpider
$mySpider->fetchPage("http://www.msn.com"); /// fetches the home page of msn.com
echo $mySpider->html; /// prints out the html to the screen

?>

有问题的具体行是,

$mySpider->fetchPage("http://www.msn.com"); /// fetches the home page of msn.com

如果您能帮助解决此问题,我将非常感激!

The error is :

Fatal error: Call to undefined method wSpider::fetchPage()

Firstly, what I'm trying to do is build a spider to get data from a webpage. I'm not exactly sure why I'm getting this error, but I'm fairly new to php so it could be something fairly obvious that I'm missing. Code:

<?php
class wSpider
{
var $ch; /// going to used to hold our cURL instance
var $html; /// used to hold resultant html data
var $binary; /// used for binary transfers
var $url; /// used to hold the url to be downloaded

function wSpider()
{
$this->html = "";
$this->binary = 0;
$this->url = “”;
}
}

function fetchPage($url)
{
$this->url = $url;
if (isset($this->url)) {
$this->ch = curl_init (); /// open a cURL instance
curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, 1); // tell cURL to return the data
curl_setopt ($this->ch, CURLOPT_URL, $this->url); /// set the URL to download
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); /// Follow any redirects
curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, $this->binary); /// tells cURL if the data is binary data or not
$this->html = curl_exec($this->ch); // pulls the webpage from the internet
curl_close ($this->ch); /// closes the connection
}
}

$mySpider = new wSpider(); //// creates a new instance of the wSpider
$mySpider->fetchPage("http://www.msn.com"); /// fetches the home page of msn.com
echo $mySpider->html; /// prints out the html to the screen

?>

The specific line in question is

$mySpider->fetchPage("http://www.msn.com"); /// fetches the home page of msn.com

I'd be very grateful for any help to resolve this issue!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的类中没有 fetchPage 方法,这就是它不起作用的原因。这就是为什么你应该缩进你的代码。试试

<?php
class wSpider
{
    var $ch; /// going to used to hold our cURL instance
    var $html; /// used to hold resultant html data
    var $binary; /// used for binary transfers
    var $url; /// used to hold the url to be downloaded

    function wSpider()
    {
        $this->html   = "";
        $this->binary = 0;
        $this->url    = “”;
    }
    function fetchPage($url)
    {
        $this->url = $url;
        if (isset($this->url)) {
            $this->ch = curl_init(); /// open a cURL instance
            curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); // tell cURL to return the data
            curl_setopt($this->ch, CURLOPT_URL, $this->url); /// set the URL to download
            curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); /// Follow any redirects
            curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, $this->binary); /// tells cURL if the data is binary data or not
            $this->html = curl_exec($this->ch); // pulls the webpage from the internet
            curl_close($this->ch); /// closes the connection
        }
    }
}


$mySpider = new wSpider(); //// creates a new instance of the wSpider
$mySpider->fetchPage("http://www.msn.com"); /// fetches the home page of msn.com
echo $mySpider->html; /// prints out the html to the screen

?>

你的课程就在这里结束

$this->url = “”;
}
} // right here

,函数在括号之后定义。

There is no fetchPage method in your class, that's why it isn't working. That's why you should indent your code. Try

<?php
class wSpider
{
    var $ch; /// going to used to hold our cURL instance
    var $html; /// used to hold resultant html data
    var $binary; /// used for binary transfers
    var $url; /// used to hold the url to be downloaded

    function wSpider()
    {
        $this->html   = "";
        $this->binary = 0;
        $this->url    = “”;
    }
    function fetchPage($url)
    {
        $this->url = $url;
        if (isset($this->url)) {
            $this->ch = curl_init(); /// open a cURL instance
            curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); // tell cURL to return the data
            curl_setopt($this->ch, CURLOPT_URL, $this->url); /// set the URL to download
            curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); /// Follow any redirects
            curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, $this->binary); /// tells cURL if the data is binary data or not
            $this->html = curl_exec($this->ch); // pulls the webpage from the internet
            curl_close($this->ch); /// closes the connection
        }
    }
}


$mySpider = new wSpider(); //// creates a new instance of the wSpider
$mySpider->fetchPage("http://www.msn.com"); /// fetches the home page of msn.com
echo $mySpider->html; /// prints out the html to the screen

?>

Your class ends right here

$this->url = “”;
}
} // right here

and function is defined AFTER that bracket.

隔岸观火 2024-12-17 22:38:25

您遇到了大括号问题,导致 fetchPage 不是您的类的成员:

function wSpider()
{
$this->html = "";
$this->binary = 0;
$this->url = “”;
}
} // This brace ends your class declaration! Move it!

您可能还需要按照建议将构造函数重命名为 __construct 。

You 've got a brace issue that causes fetchPage to not be a member of your class:

function wSpider()
{
$this->html = "";
$this->binary = 0;
$this->url = “”;
}
} // This brace ends your class declaration! Move it!

You might also want to rename the constructor to __construct, as is recommended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文