如何在OOP PHP中解析链接?

如何在OOP PHP中解析链接?,php,Php,您好,我有以下php代码: class parser { public function __construct($link) { $xml = @simplexml_load_file($link); if (!$xml) { echo 'Error while parsing the document'; exit; } } $file = urldecode($_REQUEST['ftitle']);

您好,我有以下php代码:

    class parser {
    public function __construct($link)
    {
    $xml = @simplexml_load_file($link);
    if (!$xml) {
        echo 'Error while parsing the document';
        exit;
    }
    }
$file = urldecode($_REQUEST['ftitle']);
$obj = new parser($file);
我得到了
if
部分中提到的错误

但是如果我用一种简单的方式来尝试,也就是说我尝试了无类的方法,那么整个代码都能准确地工作,没有任何问题。
thnx previous:)

您的类无效,您缺少结尾处的
}
。。。。不建议使用
@
抑制错误

$xml
将是真的,因此请改用它

 if ($xml === false) {
我是什么意思

$xml = simplexml_load_string("<body />");

var_dump(!$xml); // returns true
var_dump($xml);  // return object(SimpleXMLElement)[1] 
$xml=simplexml\u load\u字符串(“”);
变量转储(!$xml);//返回true
变量转储($xml);//返回对象(SimpleXMLElement)[1]

试试这个,我想这会对你有所帮助,因为它在我的本地主机上工作准确

class parser {
    public function __construct($link)
    {
    $xml = @simplexml_load_file($link); // replace it with the following line.
    $xml =new SimpleXMLElement($link, NULL, TRUE);
    if (!$xml) {
        echo 'Error while parsing the document';
        exit;
    } //if end
    } //constructor end
    } //class end
$file = urldecode($_REQUEST['ftitle']); //replace it with the following line.
$file = $_REQUEST['ftitle'];
$obj = new parser($file);

删除
@
simplexml加载文件前面的
以查看实际错误是什么。我已经这样做了,但在van:(并得到以下错误:警告:simplexml加载文件()[function.simplexml加载文件]:I/O警告:无法加载外部实体是,因为URL无效…并且未指向XML文件URL有效,因为我已尝试手动检查所需的文件已下载…该文件的格式为XML我已完成此操作请检查以下由meI设置的答案认为缺少大括号只是一个输入错误。@PLB添加了附加内容。@PLB所有信息…我已经做了,请检查我设置的以下答案,你说它不工作是什么意思?我自己测试过