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

时间:2011-10-19 15:12:36

标签: php curl

错误是:

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

我非常感谢有任何帮助来解决这个问题!

2 个答案:

答案 0 :(得分:3)

您的类中没有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

并且在该括号之后定义了函数。

答案 1 :(得分:1)

你有一个大括号问题导致fetchPage不属于你的班级成员:

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

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