PHP problem with DOM parsing
The code pasted below, works on my PC, but not on my hosting (which have PHP 5.2.13 installed).
$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';
$fope开发者_开发问答n_handle = fopen($xml_file, 'w+');
fwrite($fopen_handle, $xml);
fclose($fopen_handle);
On my hosting, the foreach loop doesn't executes i.e. I get only this in the XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>
Thanks in advance.
Your hosting doesn't support file_get_contents (allow_url_fopen is the setting iirc)
Try this:
$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);
精彩评论