0
I want after reading the html page with the file_get_contents
and select what matters with the preg_match_all
a is automatically generated xml (calling the function below) and generating the file.
As there are several lists to be generated in the same php, I didn’t want to have to repeat the commands of XmlWriter
, just call the function to do automatically.
<?php
header ('Content-Type:application/xml');
$html = file_get_contents('http://www.teste.com/');
$re = '/href=\'(.*?)\'>(.*?)</(.*?)';
preg_match_all($re, $html, $key);
foreach($key[1] as $i)
function lista_xml($xml){
$xml = new XMLWriter();
$xml->openMemory();
$xml->startDocument('1.0','UTF-8');
$xml->startElement('items');
$xml->startElement('lista');
$xml->writeCData('$nome');
$xml->endElement();
$xml->startElement('canal');
$xml->startElement('titulo');
$xml->writeCData('.$key[2][$i].');
$xml->endElement();
$xml->startElement('img');
$xml->writeCData();
$xml->endElement();
$xml->startElement('link');
$xml->writeCData('.$key[1][$i].');
$xml->endElement();
$xml->startElement('detalhes');
$xml->writeCData('<center><img src='".$key[3][$i]."'/> '.$key[2][$i].'</center>');
$xml->endElement();
$xml->endDocument();
echo $xml ->outputMemory();
$xml->flush();
unset($xml);
}
?>
William, thank you for your help. I tested and presented an error on line 33. Naturally due to the quotation marks. No double quotes "error on line 2 at column 1: Extra content at the end of the Document".
– Antonio Oliveira
I tested it several times but to no avail. I used a direct html text without using file_get_contents and the result is still an empty page. I didn’t want to have to use an external class for XML. E.
– Antonio Oliveira
Yes, Guilherme. I tested on 4 different servers. On a Mac (localhost) PHP 7.1, on a NAS (PHP 5.4), on a free server and in xamp (Windows). All with display_errors on. I must be doing something wrong, certainly. The error message: (This page contains the following errors: error on line 2 at column 1: Extra content at the end of the Document)
– Antonio Oliveira
@Antoniooliveira fixed the script, copy again, if the same error occurs remove this line
header ('Content-Type:application/xml');
and save the script, then press F5 in the browser will appear all errors.– Guilherme Nascimento
Hi William. Now it worked. Thank you so much for your help.
– Antonio Oliveira
@Antoniooliveira will then mark the answer correctly, please, if you do not know how to do it read this: https://pt.meta.stackoverflow.com/q/1078/3635
– Guilherme Nascimento
Only one detail was missing: What would the code look like if the xml generating part was a function (from "/Start XML"), to avoid having to repeat all the cell inclusion commands? In a news site, for example, it would have to generate 7 xml tables, which would make the code very long.
– Antonio Oliveira
@Antoniooliveira but you want to generate 7 different xmls in the same request? I believe this is not possible
– Guilherme Nascimento
No. Requests will be different. The first xml will forward to a second page and so on. It will be a different regex and naturally another request.
– Antonio Oliveira
Hi William. The job was perfect. Just what I needed. Thank you.
– Antonio Oliveira
The function was perfect. But I would like to ask another question. How do I store in memory
'titulo'
'img'
and'link'
? So that in the second table you can use:GerarXml('titulo', '#o regex da página secundária#', file_get_contents('link'));
?? how these elements are dynamically generated in the function you created, with theforeach ($data[1] as $key => $value)
, in the second request this data also need to be variable.– Antonio Oliveira