XML Feed gone wrong

Discussion in 'PHP' started by david16, Oct 12, 2010.

  1. #1
    Hi, I wonder if someone out there could help me. I am not a webdesigner I built my own site and enjoyed it. There are somethings that I just do not understand so I ask questions and learn a little. I needed an xml feed so not knowing what I was doing I paid to have one created for me. As the norm after I pay I find the feed does not work and the guy doesnt reply to emails etc. It very nearly does but the problem is with the images. The code is here -
    <?php

    header("content-type: text/xml");

    include_once("includes/config.php");

    $_DB = @mysql_connect($server, $DBusername, $DBpassword) or die(mysql_error());
    @mysql_select_db($database) or die(mysql_error());

    echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    echo ' <root>' . "\n";


    $result = mysql_query("SELECT p.*, t.`propertytype` AS `type`, l.`propertylocation` AS `location`
    FROM `$property_table` AS p
    LEFT JOIN `$propertytypes_table` AS t ON p.`propertytype` = t.`id`
    LEFT JOIN `$propertylocations_table` AS l ON p.`propertylocation` = l.`id`", $_DB) or die(mysql_error());

    while( $row = mysql_fetch_assoc($result) ) {

    $features = array();
    $fe_res = mysql_query("SELECT fe.`description`
    FROM `$propertyfeatures_table` AS f
    INNER JOIN `$features_table` AS fe ON f.`feature_id` = fe.`id`
    WHERE f.`property_id` = '" . $row['id'] . "'");
    while( $fe_row = mysql_fetch_assoc($fe_res) ) { $features[] = $fe_row['description']; }

    echo ' <property>' . "\n";

    echo ' <reference>' . $row['propertyref'] . '</reference>' . "\n";
    echo ' <price>' . $row['propertyprice'] . '</price>' . "\n";
    echo ' <type>' . $row['type'] . '</type>' . "\n";
    echo ' <location>' . cleanString($row['location']) . '</location>' . "\n";
    echo ' <complex>' . cleanString($row['propertyaddress']) . '</complex>' . "\n";
    echo ' <bedrooms>' . $row['propertybedrooms'] . '</bedrooms>' . "\n";
    echo ' <bathrooms>' . $row['propertybathrooms'] . '</bathrooms>' . "\n";
    echo ' <pool>' . ( in_array('Swimming Pool', $features) ? '1' : '0' ) . '</pool>' . "\n";
    echo ' <interior>' . ( strlen($row['propertylivingarea']) > 0 ? cleanString($row['propertylivingarea']) : 'Unknown' ) . '</interior>' . "\n";
    echo ' <exterior>' . ( strlen($row['propertyplotsize']) > 0 ? cleanString($row['propertyplotsize']) : 'Unknown' ) . '</exterior>' . "\n";
    echo ' <description>' . cleanString($row['longdescription']) . '</description>' . "\n";
    echo ' <furnished>' . ( in_array('Sold Furnished', $features) ? '1' : '0' ) . '</furnished>' . "\n";
    echo ' <terrace>' . ( in_array('Terrace', $features) ? '1' : '0' ) . '</terrace>' . "\n";
    echo ' <garage>' . ( in_array('Garage', $features) ? '1' : '0' ) . '</garage>' . "\n";
    echo ' <lift>' . ( in_array('Lift', $features) ? '1' : '0' ) . '</lift>' . "\n";
    echo ' <panoramic>' . ( in_array('Panoramic Views', $features) ? '1' : '0' ) . '</panoramic>' . "\n";
    echo ' <mountain>' . ( in_array('Mountain Views', $features) ? '1' : '0' ) . '</mountain>' . "\n";
    echo ' <sea>' . ( in_array('Sea Views', $features) ? '1' : '0' ) . '</sea>' . "\n";
    echo ' <garden>' . ( in_array('Garden Views', $features) ? '1' : '0' ) . '</garden>' . "\n";

    for( $i = 1; $i <= 10; $i++ ) {

    if( strlen($row['propertyphoto' . $i]) > 0 )
    echo ' <image_' . $i . '>http://www.tenerifepropertyportal.com/photos/' . urlencode($row['propertyphoto' . $i]) . '</image_' . $i . '>' . "\n";

    }

    echo ' </property>' . "\n";

    }


    echo ' </root>';

    function cleanString($text) {

    $text = stripslashes($text);
    $text = htmlentities($text, ENT_COMPAT, 'UTF-8');
    $text = str_replace('€', '&euro;', $text);
    $text = str_replace('&', '&amp;', $text);
    $text = mb_convert_encoding($text, 'UTF-8');
    // $text = urlencode($text);

    return $text;
    }

    ?>

    Not understanding the code I think I have narrowed the problem to this line here -

    echo ' <image_' . $i . '>http://www.tenerifepropertyportal.com/photos/' . urlencode($row['propertyphoto' . $i]) . '</image_' . $i . '>' . "\n";
    }

    For an example it is outputting this url
    http://www.tenerifepropertyportal.com/photos/D7040-Valley+view.jpg
    which gives a 404 when it should be outputting this url
    http://www.tenerifepropertyportal.com/photos/D7040-Valley view.jpg

    The only difference is the white space between Valley view and when you put that url into a browser you get -
    http://www.tenerifepropertyportal.com/photos/D7040-Valley%20view.jpg

    which works fine. I think it just needs the " + " removing but I dont know how, is there anyone that could help me with my problem?

    Thanks
     
    david16, Oct 12, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    If this is all you need to do, you can simply remove the urlencode function from the above - or use rawlurlencode instead;

    echo ' <image_' . $i . '>http://www.tenerifepropertyportal.com/photos/' . rawurlencode($row['propertyphoto' . $i]) . '</image_' . $i . '>' . "\n";
    Code (markup):
     
    lukeg32, Oct 12, 2010 IP
  3. david16

    david16 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Lukeg32
    Thank you so much for that, I added raw to where you told me and uploaded the feed and it works perfectly. I am so appreciative it makes a change to find someone who can help like you have. Now the photos are displaying all I have to do now is to get the other site to pick up the feed and update my listings and I will have photos in all my property listings. Thanks again
     
    david16, Oct 12, 2010 IP
  4. david16

    david16 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi Lukeg32
    Again thanks for your help. I have just been checking the feed and most of the url's for the properties are fine I am finding a few that are not working as the url generated seems to be very long
    <image_1>http://www.tenerifepropertyportal.com/photos/..%2F..%2Fphotos%2F22525%2F2240557_large.jpg</image_1>
    <image_2>http://www.tenerifepropertyportal.com/photos/..%2F..%2Fphotos%2F22525%2F2145319_large.jpg</image_2>
    <image_3>http://www.tenerifepropertyportal.com/photos/..%2F..%2Fphotos%2F22525%2F2145320_large.jpg</image_3>
    <image_4>http://www.tenerifepropertyportal.com/photos/..%2F..%2Fphotos%2F22525%2F2145321_large.jpg</image_4>
    <image_5>http://www.tenerifepropertyportal.com/photos/..%2F..%2Fphotos%2F22525%2F2145322_large.jpg</image_5>
    <image_6>http://www.tenerifepropertyportal.com/photos/..%2F..%2Fphotos%2F22525%2F2145323_large.jpg</image_6>
    <image_7>http://www.tenerifepropertyportal.com/photos/..%2F..%2Fphotos%2F22525%2F2145324_large.jpg</image_7>
    <image_8>http://www.tenerifepropertyportal.com/photos/..%2F..%2Fphotos%2F22525%2F2145325_large.jpg</image_8>
    <image_9>http://www.tenerifepropertyportal.com/photos/..%2F..%2Fphotos%2F22525%2F2145326_large.jpg</image_9>

    Any ideas on what I could do to shorten the url so they would display. Really sorry to bother you but I am afraid I just do not know what I am doing here

    Thanks

    David16
     
    david16, Oct 12, 2010 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Hiya,

    The rawurlencode function will take any non-alphanumeric character (with a few exceptions) and replace it with the corresponding HTML entity;

    If you use spaces and symbols in your image names, you will end up with the above - %2F is the hex digits for a space, as an example.

    You could try removing the rawurlencode function, though im not sure what effects this may have (depending on the characters you use). There are
    benefits of using this function, such as to prevent 'special' characters used in URL's being read literally.

    Something like this;

    echo ' <image_' . $i . '>[url]http://www.tenerifepropertyportal.com/photos/[/url]' . $row['propertyphoto' . $i] . '</image_' . $i . '>' . "\n";
    PHP:
    If you want me to take a closer look for you, chuck me a PM.
     
    lukeg32, Oct 12, 2010 IP