Jump to content

converting a php file to a xml feed


arcanine

Recommended Posts

hi there, I'm running a forum (smf) and decided I wanted to have a

coverflow application

http://dinogod.adrianhosting.com/smf/index.php?action=showcase

you can see in action above, although there is a problem with it the

links to the pictures are hardcoded so if I wanted a new image I have to recompile it in

flex builder 3 I've asked around in flex forums as to a function to scan the image directory

for new images but it has to be done in php then flex can consume it in xml format

 

So on to my php problem I'm using a code snippet from phparadise

which lists all images in my directory and echos each filename per line

you can see the script live here http://dinogod.adrianhosting.com/smf/coverflow/list.php

the problem is flex won't consume a standard php script so I need help in generating

a xml feed from script for reference here is the source of the script

 

<?php

$imgdir = 'img/'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show

$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
{
  $a_img[] = $imgfile;
  sort($a_img);
  reset ($a_img);
} 
}

$totimg = count($a_img); // total image number

for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);

// do whatever
$halfwidth = ceil($size[0]/2);
$halfheight = ceil($size[1]/2);


echo "$a_img[$x] <br />";


}

?>

I've tried learning how to do this myself (on wc3.org) but either nothing shows at all, or xml complains at me furiously

can someone take me through it, I'm not a php programmer by trade and need clear guidance, or an example

thanks for your time  :)

 

 

Link to comment
Share on other sites

I dont think feed is the correct term. All you need is to use php to create an xml. The file extention would still be php only it would have xml data in it so flex can read the file.

 

i dunno what that last forloop does but replace this

<?
for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);

// do whatever
$halfwidth = ceil($size[0]/2);
$halfheight = ceil($size[1]/2);


echo "$a_img[$x] <br />";


}
?>

 

with

<gallery>
<?php
foreach($a_img as $imageUrl){
?>
    <image>
        <url><?php echo($imageUrl) ?></url>
    </image>
<?php
}
?>
</gallery>

well something like that it really depends on how you want your xml to look its best to use a static xml first.

Link to comment
Share on other sites

Thanks for your reply I tried your suggestion and the php file

becomes blank so I did a little testing it seems $x needs to be

echoed for the filename to come out I tried echoing

 

"$a_img"

on its own and as the output I got

 

Array

Array

 

but no file names just the word array, so $a_img and $x need to be merged or

something to make your script work how would I go about doing this

because I see you've changed $a_img to $imageUrl but it doesn't have all the information

in that string

 

also some of that code can be ignored the original script would output image size and other details

I didn't need so I changed the echo however some of the original scripting will be in there

 

thanks again for your help I appreciate it

Link to comment
Share on other sites

okay I've been working non stop on this but not really getting anywhere here is where I'm at thus far

 

<?php //define the xml standard
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
?>
<?php //this section calculates the image names in the directory

$imgdir = 'img/'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show
$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
{
  $a_img[] = $imgfile;
  sort($a_img);
  reset ($a_img);
} 
}

$totimg = count($a_img); 

for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);
?>


<?php //echo xml tree
echo "<gallery>";
echo "<image>";
echo "<url>$a_img[$x]"; 
echo "</url>";
echo "</image>";
echo "</gallery>";


}

?>

 

whichoutputs

 

<?xml version='1.0' encoding='ISO-8859-1'?><gallery><image><url>11.jpg</url></image></gallery><gallery><image><url>12.jpg</url></image></gallery>

 

which is of course wrong I need the url tags to repeat not gallery and images

you can see the errors for yourself at

 

http://dinogod.adrianhosting.com/smf/coverflow/list.php

 

how can I say only print gallery and image tags once but the url tags as many times as the string needs

Link to comment
Share on other sites

Try:

<?php
//define the xml standard
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";

//this section calculates the image names in the directory
$imgdir = 'img/'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show

$dimg = opendir($imgdir);

$url = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $imgdir;

echo "<gallery>
  <image>\n";

while($imgfile = readdir($dimg))
{
    if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
    {
        echo '    <url>' $url . $imgfile . "</url>\n";
    }
}

echo '  </image>
<gallery>';

?>

Link to comment
Share on other sites

I imagine I don't if I'm not seeing anything

 

I don't know how to find out whether error_reporting is on or off

as I've mentioned I don't know php  :-\

 

your error does mention line 21 though which is

 

        echo '    <url>' $url . $imgfile . "</url>\n";

does anything look wrong there?

Link to comment
Share on other sites

I missed a period from my code, I forgot to correct it before I posted the code. Fixed code

<?php
//define the xml standard
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";

//this section calculates the image names in the directory
$imgdir = 'img/'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show

$dimg = opendir($imgdir);

$url = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $imgdir;

echo "<gallery>
  <image>\n";

while($imgfile = readdir($dimg))
{
    if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
    {
        echo '    <url>' . $url . $imgfile . "</url>\n";
    }
}

echo '  </image>
<gallery>';

?>

If you still get a blank screen then you should turn on display_errors

Link to comment
Share on other sites

oh right, thank you for all this by the way

 

the corrected script is at

 

http://dinogod.adrianhosting.com/smf/coverflow/test.php

 

I get XML

Parsing Error: no element found

Location: http://dinogod.adrianhosting.com/smf/coverflow/test.php

Line Number 6, Column 10:

 

looking at the source the output is

 

<?xml version='1.0' encoding='ISO-8859-1'?><gallery>

  <image>

    <url>http://dinogod.adrianhosting.com/img/11.jpg</url>

    <url>http://dinogod.adrianhosting.com/img/12.jpg</url>

  </image>

<gallery>

which to me looks right, can I ignore that xml error and start trying to call the

xml file to my flex project? or is it not valid yet?

Link to comment
Share on other sites

Thanks a lot  :D just what I needed I'll be sure to bookmark this

forum in google bookmarks

 

Final script just in case anyone wanted something similar

 

<?php
//define the xml standard
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";

//this section calculates the image names in the directory
$imgdir = 'img/'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show

$dimg = opendir($imgdir);

$url = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $imgdir;

echo "\n<gallery>
  <image>\n";

while($imgfile = readdir($dimg))
{
    if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
    {
        echo '    <url>' . $imgfile . "</url>\n";
    }
}

echo '  </image>
</gallery>';

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.