php de referencia de entidad sin terminar


Aquí está mi código:

<?php
// 27/01/2016 Edit:
$result = mysql_query("A Long mysql query");
$rss = new SimpleXMLElement('<rss version="2.0" />');
$products = $rss->addChild('products');
///
while($row = mysql_fetch_array($result)){
$product = $products->addChild('category');
$product->addChild('product_id',"$row[product_id]");
$product->addChild('cat_id',"$row[cat_id]");
$product->addChild('cat_name',"$row[cat_name]");
$product->addchild('product_code',"$row[product_code]");
$product->addchild('product_name',"$row[product_name]");
$product->addChild('description','$row[description]');
$product->addchild('rating',"$row[rating]");
$product->addchild('image_url','$row[imag_url]');
$product->addchild('price',"$row[price]");
$product->addchild('discount',"$row[discount]");
$product->addchild('stock_status',"$row[stock_status]");
$product->addchild('stock_quantity',"$row[stock_quantity]");
$product->addchild('weight',"$row[weight]");
$product->addchild('length',"$row[length]");
$product->addchild('width',"$row[width]");
$product->addchild('height',"$row[height]");
$product->addchild('colour',"$row[colour]");
$product->addchild('size',"$row[size]");
$product->addchild('material',"$row[material]");
$product->addchild('pattern',"$row[pattern]");
};

Header('Content-type: text/xml');
print($rss->asXML());
?>

Y aquí está el error:

Advertencia: SimpleXMLElement::addChild() [simplexmlelement.addchild]: referencia de entidad sin terminar _Coke.jpg en C:\wamp\www\rabwah\core.php en línea 40

El error está en la línea con '$row[imag_url]'.

Author: Mehravish Temkar, 2013-06-10

6 answers

Esto codifica correctamente el & < > y "" ''

$parent->addChild($name, htmlspecialchars($value));
 81
Author: Joel Davey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-10 14:36:12

SimpleXMLElement es en realidad un recurso del sistema que se comporta como un objeto. Lo que hace que trabajar con bucles sea complicado. Así que al intentar agregar nuevos elementos secundarios en lugar de esto:

$product->addchild('element', $value);

Haz esto:

$product->element = $value;

O puede usar htmlspecialchars(), para escapar caracteres html.

Nota:

mysql_* está en desuso como de php 5.5 y eliminada a partir de php-7. Así que en su lugar use mysqli_* o PDO.
¿Por qué no debería usar las funciones mysql_* en PHP?

 37
Author: mega6382,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-18 05:34:05

Mi solución a esto es crear específicamente un nodo de texto, que se asegura de que absolutamente todo se escape correctamente.

$cell = $dom->createElement('td');
$cell->appendChild($dom->createTextNode($value));
 4
Author: Kavi Siegel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-17 15:35:15

Lo siento por revivir una vieja pregunta, pero hay otra solución a esto.. Suponiendo que el siguiente código causa el error "referencia de entidad sin terminar":

$xml->addChild($key,$value); 

La solución de@Joel-Davey funciona muy bien:

$xml->addChild($key,htmlspecialchars($value)); 

Pero también puede hacer lo siguiente si, por alguna razón, no desea usar la función htmlspecialchars anterior (básicamente, divide el paso uno en dos pasos):

$xml->addChild($key); 
$xml->$key=$value; 

No tengo idea de cuál se ejecutará más rápido; dudo que haría mucho de un diferencia, pero, esto funciona, y pensé que debería mencionarse

PD: sé que funciona porque lo estoy usando en un proyecto personal

 1
Author: Peter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-07 08:08:00

Prueba cambiando -

$product->addchild('image_url','$row[imag_url]');

A

$product->addchild('image_url',"$row[\"imag_url\"]");

O

$product->addchild('image_url',$row['imag_url']);

EDITAR wrap quotes too round image_url, courtesy Barrmar

 0
Author: swapnesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:02:43

La forma correcta es:

$product->addchild('image_url',htmlspecialchars($row['imag_url']));
 0
Author: Eolia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-24 17:01:22