致命错误:调用未定义的方法 wSpider::fetchPage()
错误是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的类中没有 fetchPage 方法,这就是它不起作用的原因。这就是为什么你应该缩进你的代码。试试
你的课程就在这里结束
,函数在括号之后定义。
There is no fetchPage method in your class, that's why it isn't working. That's why you should indent your code. Try
Your class ends right here
and function is defined AFTER that bracket.
您遇到了大括号问题,导致 fetchPage 不是您的类的成员:
您可能还需要按照建议将构造函数重命名为 __construct 。
You 've got a brace issue that causes
fetchPage
to not be a member of your class:You might also want to rename the constructor to
__construct
, as is recommended.