TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Return XML From Function (http://www.talkphp.com/general/1996-return-xml-function.html)

aristoworks 01-18-2008 02:50 AM

Return XML From Function
 
I'm working with a flash developer and using AMFPHP on the back end .

I've got a class with a function that creates a thumbnail.

I am needing to return a very simple xml string:

Quote:

<imageCacheResult>

<url>$url</url>

<thumbnail>$thumbnail</thumbnail>

</imageCacheResult>
but I'm having some difficulties.

Can anyone point me in the right direction.

Wildhoney 01-18-2008 03:09 AM

Having difficulties generating it or parsing it?

aristoworks 01-18-2008 08:05 PM

generating

sketchMedia 01-18-2008 08:11 PM

in what way are you having hard time with? i.e. you dont have a clue how to start or are you getting an almighty error?

sketchMedia 01-18-2008 08:37 PM

i'll just assume that you dont know how to do it, PHP DOM to the rescue....again.

ach! if i have to code one more php DOM script today i think i may go completely mad ,actually thats a lie, as its already happend EHHHEHEHHHHAHAHAHAHAAH

*clears throat*

anyway back to php for a moment if we may (how ever funny insanity may be)

PHP Code:

$pDomDoc = new DomDocument('1.0');
//create root node <imageCacheResult />
$root $pDomDoc->appendChild($pDomDoc->createElement('imageCacheResult'));

//create <url /> node, then add a text node to it
$url $root->appendChild($pDomDoc->createElement('url'));
$url->appendChild($pDomDoc->createTextNode('value of url'));

//create <thumbnail />, then append a text node to it
$thumb $root->appendChild($pDomDoc->createElement('thumbnail');
$thumb->appendChild($pDomDoc->createTextNode('value of thumbnail'));

//save the xml
return $pDomDoc->saveXML(); 

that should work, hopefully and return this xml in a string:
xml Code:
<?xml version="1.0" ?>
<imageCacheResult>
    <url>value of url</url>
    <thumbnail>value of thumbnail</thumbnail>   
</imageCacheResult>
if you need to specifiy a charset, just pass it into the second param in the DomDocument construct i.e.:
PHP Code:

$pDomDoc = new DomDocument('1.0''UTF-8'); 

hope that helps
Im off to continue my insanity now gooooooooooooooobyyyyyyyyyyyeeeeee

Alan @ CIT 01-18-2008 10:15 PM

Going on your code sample, could you not just do something like:

PHP Code:

<?php

// ... your code here ...

$xmlResponse "<imageCacheResult>";
$xmlResponse .= "<url>$url</url>";
$xmlResponse .= "<thumbnail>$thumbnail</thumbnail>";
$xmlResponse .= "</imageCacheResult>";

return 
$xmlResponse;

?

Alan

sketchMedia 01-18-2008 10:19 PM

he could, but where would the fun be i ask you???????

sketchMedia 01-18-2008 10:32 PM

yea, alan's example is better suited to your needs atm, but if you needed multiple 'imageCacheResults' PHP DOM is the way to go, it will get messy pratting around with strings and loops and god knows what, although you would have to create a new root, i.e.:

xml Code:
<?xml version="1.0"?>
<imageCacheResults>
    <imageCacheResult>
        <url>value of url</url>
        <thumbnail>value of thumbnail</thumbnail>
    </imageCacheResult>
    <imageCacheResult>
         <url>value of url</url>
         <thumbnail>value of thumbnail</thumbnail>
    </imageCacheResult>
</imageCacheResults>

bah humbug im going to bed, im far to tired my brain is farting out errors again

aristoworks 01-18-2008 10:37 PM

Hey Guys,

Sorry for not being so clear - I couldn't figure out what in the world was going on but after fooling with it I was able to pinpoint what is happening.

I've got a thumbnail creation script that fires off prior to the xml output so I'm having to set a content type header twice - and it's not working the second time. Is there any way to avoid this conflict?

Thanks
jw

RobertK 01-18-2008 11:07 PM

Yeah, save the image to a file! You cannot output two separate content types within the same request. So, cache the thumbnail and link to that in your thumbnail parameter. Else, you'll have to turn on the override switch (header).


All times are GMT. The time now is 01:54 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0