How to convert youtube api data to xml data feed using php? - php

I have an idea to convert this JSON data:
https://www.googleapis.com/youtube/v3/videos?part=snippet&chart=mostPopular&maxResults=50&regionCode=us&videoCategoryId=10&key=Mykey
to xml ,To use for feeds Rss reader
How to do this using PHP only ?
My respect

Youtube API only returns JSON. You can load the json in your code and implement your own XML using
header('Content-type: text/xml');
// other headers are required
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
echo '<xml>';
// Your data
echo '</xml>';

Related

php xml SimpleXMLElement addChild big data breaking xml

I am trying to insert base64_encoded pdf content in xml with below code.
1) Getting pdf data in variable.
$varFilePath = 'temp_invoices/' . $varFileName;
$arrFileData = file_get_contents($varFilePath);
$varPdfBase64 = base64_encode($arrFileData);
2) Using base64 data in xml tag
$Attachment->addChild('Attachment', $varPdfBase64);
The variable $varPdfBase64 has too big content and its creating problem.
Now when I am trying to download xml file this is showing plain text instead downloading with xml tags.
Note:- All tags are present in source code(ctrl+u).
How can I resolve this issue?

create and download xml with button and domdocument

well what i wnat to do is create a xml file and download it, if it is possible not saving in the server.
what i have so far is creating the xml with DOMDocument and store it in the server
<?php
$domTree = new DOMDocument('1.0', 'UTF-8');
$rootXML = $domTree->createElement( 'XML' );
$rootXML = $domTree->appendChild($rootXML);
$personalData = $domTree->createElement( 'PERSONAL' );
$personalData = $rootXML->appendChild($personalData);
$personalData ->nodeValue = "Alan";
$domTree->save('MyXmlFile.xml');
?>
i know i supposed to use something like this
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');
echo $xml_contents;
?>
but using DOMDocument im lost in hwo using it, THANKS IN ADVANCE!
To output to the standard output - that is what PHP sends to the browser - you can make use of the PHP output stream:
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');
$domTree->save('php://stdout');
This principle work with (nearly) anything in PHP that expects a filename which is a pretty nice feature. Compare for example with my answer to phpexcel to download.
It is merely the same as when you would do:
echo $domTree->saveXML();
as echo as well "prints" to the standard output. But for larger data, the concept of a stream is preferable.
If you want to learn more about standard output, Wikipedia has an overview about these so called standard streams:
Standard streams
For the correct XML content-type, this depends a bit what you do, I suggest taking a look into if you like to learn a bit more:
What Content-Type value should I send for my XML sitemap?

Getting the content of an xml file creates an XML Parsing Error

I am trying to get the contents of an XML file in case that the file exists or I am generating a new xml file. The problem is that when I am trying to get the xml file I get this error:
XML Parsing Error: no element found Location:
http://mydomain.gr/generate.php Line Number 1, Column 1: ^
My code is this
<?php
include_once('xmlgenerator.php');
$xml = new XmlGenerator();
if($xml->cachefile_exists){
if(!$xml->is_uptodate()){
// echo "update it now";
$xml->createFile = 1;
$data = $xml->create();
Header('Content-type: text/xml');
print($data->asXML());
}else{
//echo "doesn't need any update.";
Header('Content-type: text/xml');
file_get_contents($xml->cached_file);
}
}else{
// echo "Didn't find any cache file. Lets Create it";
$xml->createFile = 1;
$data = $xml->create();
Header('Content-type: text/xml');
print($data->asXML());
}
?>
The XML structure is fine, and I double check about the XML file encoding or the php file that call the XML. Everything is UTF8 without BOM. When I open the XML file directly in a browsers it looks perfect and its a valid file ( checked it with w3c and online tools).
the 2 lines that create the problem(most probably):
Header('Content-type: text/xml');
file_get_contents($xml->cached_file)
I even deleted anything and just used this 2 lines and got the same error.
So what can be wrong? Is there any proper way to include an XML file in a php file, change the headers and show it to the end user? I really don't want to redirect, I need to stay to the same php file just show XML content.
echo file_get_contents($xml->cached_file)

create empty spreadsheet in google docs

i want to create a empty spreadsheet with google docs api but i get the error: could not convert document.
$xml = "--END_OF_PART\r\n".
"Content-Type: application/atom+xml;\r\n\r\n".
"<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:docs=\"http://schemas.google.com/docs/2007\">
<category scheme=\"http://schemas.google.com/g/2005#kind\"
term=\"http://schemas.google.com/docs/2007#spreadsheet\"/>
<title>".$name."</title>
</entry>\r\n".
"--END_OF_PART\r\n".
"Content-Type: text/csv;\r\n\r\n".
"--END_OF_PART--\r\n";
This xml is sent to the server of google with a post with param convert=true
Then i got the error: could not convert document.
When i use the mime-type 'application/vnd.oasis.opendocument.spreadsheet' instead of 'text/csv' and do not add the param convert=true i get an internal error.
Docs List API v3 is deprecated. Use Google Drive API instead. You can create a file with the insert call:
https://developers.google.com/drive/v2/reference/files/insert
the mimeType for a native spreadsheet would be:
application/vnd.google-apps.spreadsheet

php rest webservice

how can i get a xml file when i pass a parameterized url
If you are generating the XML file in PHP, try setting the header as the first line of the script.
header("Content-type: text/xml");

Categories