dongzhong5182163
2011-10-19 15:12
浏览 53
已采纳

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

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!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douleng0794 2011-10-19 15:14
    已采纳

    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.

    打赏 评论
  • dongzuo9096 2011-10-19 15:15

    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.

    打赏 评论

相关推荐 更多相似问题