普通网友 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?