Difference in reading XML from manually copied and software-generated PHP source code

I made PHP coding using an XML file, the source code of which I copied manually, it looks like

<title type='text'>content I've extracted</title> <content type='text'>content I've extracted</content> 

Now everything is done, and when I generate content using PHP coding, and when I try to extract things from the headers and content tags, the result is not generated ... when I crossed, I found a PHP-generated file (source code, RSS feed) looks like

 <title type=\'text\'>content to be extracted </title> <content type=\'text\'>content to be extracted</content> 

As there are backslashes, it cannot extract the contents, I think

The sample PHP code that I use to retrieve the content from these tags is

  $titles = $entry->getElementsByTagName( "title" ); $title = $titles->item(0)->nodeValue; $descrs = $entry->getElementsByTagName( "content" ); $descr = $descrs->item(0)->nodeValue; 

Can someone please suggest me how I can proceed.

This is the PHP code I used to create the XML

 $url='http://gdata.youtube.com/feeds/api/playlists/12345'; $fp = fopen($url, 'r'); $buffer=''; if ($fp) { while (!feof($fp)) $buffer .= fgets($fp, 1024); fclose($fp); file_put_contents('feed.xml', $buffer); 

I found a solution

 $buff=stripslashes($buffer); file_put_contents('ka.xml', $buff); 

so the stripslashes() function removes the backslash and works

+4
source share
1 answer

Looks like you included magic quotes .

If the magic_quotes_runtime parameter is enabled, most functions that return data from any external source, including databases and text files, will have quotes that are discarded using a backslash.

Therefore, when you use fgets to read in a file, any quotation marks will be escaped. Magic quotes are deprecated by PHP 5.3. You should not use them in your script.

Also see http://www.php.net/manual/en/security.magicquotes.php


In the clip, your approach to copying a file is much more complicated than necessary. All of them will work to save the remote XML to a file:

 $src = 'http://gdata.youtube.com/feeds/api/playlists/E6DE6EC9A373AF57?v=2'; copy($src, 'dest.xml'); 

or

 $src = 'http://gdata.youtube.com/feeds/api/playlists/E6DE6EC9A373AF57?v=2'; file_put_contents('dest.xml', file_get_contents($src)); 

or

 $src = 'http://gdata.youtube.com/feeds/api/playlists/E6DE6EC9A373AF57?v=2'; stream_copy_to_stream(fopen($src, 'r'), fopen('dest.xml', 'w+')); 
+4
source

Source: https://habr.com/ru/post/1393358/


All Articles