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

Php 致命错误:调用未定义的方法wSpider::fetchPage(),php,curl,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

错误是:

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

?>

我将非常感谢任何帮助解决这个问题

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

?>

函数是在括号后定义的。

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

?>

函数是在该括号之后定义的。

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

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

您可能还希望按照建议将构造函数重命名为
\uu construct

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

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

您可能还希望按照建议将构造函数重命名为
\uu construct

否,这将导致括号不匹配。。。OP需要将fetch page函数移到his中class@LaurenceBurke:我想即使是一个完全的新手也会意识到这一点。@LaurenceBurke:现在应该没问题了,但在这里学究气真的没有什么好处。不,那会使括号不匹配。。。OP需要将fetch page函数移到his中class@LaurenceBurke:我想即使是一个完全的新手也会意识到这一点。@LaurenceBurke:现在应该没问题了,但在这里学究气真的没什么好处。