下面粘贴的代码可以在我的电脑上运行,但不能在我的主机上安装(安装了PHP 5.2.13)。
$source = file_get_contents('http://example.com/example', 0);
$dom = new DOMDocument;
@$dom->loadHTML($source);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[@class="item"]');
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n<root>\r\n";
foreach($tags as $tag)
$xml .= "\t<tag>" . trim($tag->nodeValue) . "</tag>\r\n";
$xml .= '</root>';
$xml_file = 'tags.xml';
$fopen_handle = fopen($xml_file, 'w+');
fwrite($fopen_handle, $xml);
fclose($fopen_handle);
在我的托管上,foreach循环不会执行,即我只在XML中获得这个:
<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>
提前致谢。
答案 0 :(得分:0)
您的托管不支持file_get_contents(allow_url_fopen是设置iirc)
试试这个:
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, 'http://example.com');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);