我正在使用DOM+php从html网页中提取一些部分,并试图将结果发送到其他页面作为$_SESSION变量,并最终更新mysql数据库。
HTML网页代码示例:
<html>
<body>
<div id="title">some title </div>
<div id="city">some city</div>
<div id="country">some country</div>
<div id="company">some company</div>
<div id="text">some text</div>
<body>
<html>
这是我用来获取数据的代码,正在工作…我可以回显$var:
<?php session_start(); ?>
---- some HTML---
<?
include('simple_html_dom.php');
$file = 'webpage.html';
$html = new simple_html_dom();
$html->load_file($file);
$title = $html->getElementById('title');
$city = $html->getElementById('city');
$country = $html->getElementById('country');
$company = $html->getElementById('company');
$text= $html->getElementById('text');
echo '<b>'.$title.'</b>';
$_SESSION['title'] = $title;
echo '<b>'.$city.'</b>';
$_SESSION['city'] = $city;
echo '<b>'.$country.'</b>';
..............
?>
我的问题是,我不能发送这个$var ($title,$city,…)到任何其他php页面使用$_SESSION…我得到这个错误:
Catchable fatal error: Object of class __PHP_Incomplete_Class could not be converted to string
根据PHP文档,会话只能包含可以序列化的数据。
当PHP关闭时,它将自动获取
$_SESSION
超全局变量的内容,将其序列化,并使用会话保存处理程序将其发送到存储。
<子>来源:http://php.net/manual/en/session.examples.basic.php 子>
看起来DOM元素不幸不能被序列化,因此不能正确地存储在会话中。
UPDATE:看起来可以通过将DOM元素强制转换为字符串来解决这个问题:
$_SESSION['title'] = (string)$title;
$_SESSION['city'] = (string)$city;