Search

APIPage updated to version 0.1.7 on 7th of February 2013

  • add jsonp output
  • output formats can now be triggered by HTTP Accept header (Accept: application/json, etc)

output formats can now be triggered by HTTP Accept header

This sounds neat! Look forward to needing a use for this extension! Maybe just for ajax content...

APIPage updated to version 0.1.8 on 8th of February 2013

  • Fixes: allow acceptheader to override default format only when configured

Quick heads up: APIPage now offers two helper functions that can be utilized within a template

ApiHelper::simpleJson:

  • simple xmlnode to JSON string conversion, omitting xml attributes

ApiHelper::toJson:

  • basically same as above, but takes full advantage of the XmlToArray parser class

Both helper methods are applicable on the current node.

Found it useful while working on a recent Symphony/Angular project.

Enjoy. :)

Is it possible to output something like that:

{
"a" {...},
"b" {...}
}

The issue is that it seems to me the xml cannot be well formatted, the following gives me an error.

<a>...</a>
<b>...</b>

Is it possible without wrapping everything in another tag?

a is a html element.

But, despite of this, you've already answered your question. xml needs a root node to be valid.

and b is bold, but it was just an example. so there is no way to bypass the xml validation? I will probably have to use content type mapping to be free output as i need. :S

Thanks for your quick answer. Simple idea, for example, can I imagine something like that:

<noname>
<a>...</a>
<b>...</b>
</noname>

Then delete the name of the tag noname in the output? to have

{
"a" {...},
"b" {...}
}

An idea of how i can achieve that?

i see on this blog that simplexml does't process the root element, so i could have multiple elements in the output, but i don't know exactly how to modify xmltoarray.php

Using Symphony, you can output nearly anything without digging in PHP. With XSLT, I generate CSS, JavaScript and JSON pages dynamically. In your template, just set the output method to "text":

<xsl:output method="text"
  omit-xml-declaration="yes"
  encoding="utf-8"/>

Then you can "write" everything you want.

@vincentd that's why there's this line https://github.com/iwyg/apipage/blob/master/lib/xmltoarray.php#L51

@michael-e doesn't matter in this case. It must be valid xml.

@michael-e, sure I will do it only if I don't find the solution with api page, I feel php is probably more efficient in this case (it is only a feeling).

@iwyg, so I tried few things to process the xmltojson only on the childs and not the root element of my xml page L49/51

 $root = $xmlObj->xpath('child::node()');
 $data = $this->parseXMLOBJ($xmlObj);
 return array($root => $data);

And L34

$this->xpath('child::node()') = $xml;

Didn't work, am I on the right direction? or should i try to replace xpath('child::node()') with children()? Or... I found this here but I am not sure about how to use it:

displayChildrenRecursive($sitemap);
function displayChildrenRecursive($xmlObj,$depth=0) {
   foreach($xmlObj->children() as $child) {
      echo str_repeat('-',$depth).">".$child->getName().": ".$subchild."
";
      displayChildrenRecursive($child,$depth+1);
   }
}

@vincentd xsl json formatters are garbage (at least v1), forget about this.

I've just posted you the line in question, don't know what's so unclear about it.

return array($root => $data);

If you don't wan't the root node key to be included change the line to

return $data;

@vincentd but you know that returning json arrays is a bad idea in general?

Thank you iwyg! :-D It s exactly what i needed!

No i didn't know... Why is it a bad idea?

Vulnerability. http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx/ http://haacked.com/archive/2009/06/25/json-hijacking.aspx/

It s ok nothing will be secret in the json. All the content will be used to be displayed in a page that will be public. Thanks again for your help with your extension.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details