0

We have this issue on uploading an XML file on the Facebook catalogue. They are no longer accepting the file because the XML feed is generated on line 2 and Facebook has a limit of 5242880 bytes per line. [current XML file][1] [1]: https://i.stack.imgur.com/ETqNh.png

It seems this is because in our php we are using SimpleXMLElement. See the code below. However, we are not that good in PHP. How can we change the function so when feed is generated and downloaded, the listing will display in multiple lines?

Though, I found a possible solution from this PHP SimpleXML new line but should we only replace the string SimpleXMLElement to DOMDocument and insert the other codes?

Thanks,

public static function update_facebook_ads_feed( $properties, $first_update = false, $last_update = false ) {
            write_log( 'STARTED FACEBOOK ADS FEED UPDATE' );
            $upload_dir       = wp_get_upload_dir();
            $content_url      = $upload_dir['basedir'] . '/';
            $temp_fb_xml_file = $content_url . 'fb-temp-feed.xml';
            $main_fb_xml_file = $content_url . 'fb-ad-feed.xml';

            if ( $first_update ) {
                $listings = new SimpleXMLElement( "<listings></listings>" );
                $listings->addChild( 'title', get_bloginfo( 'name' ) . ' Feed' );
                $link = $listings->addChild( 'link' );
                $link->addAttribute( 'rel', 'self' );
                $link->addAttribute( 'href', home_url() );
            } else {
                $listings = simplexml_load_file( $temp_fb_xml_file );
            }
            if ( ! $last_update ) {
                if ( ! empty( $properties ) ) {
                    foreach ( $properties as $property ) {
                        $lat  = $property->latitude;
                        $long = $property->longitude;

                        if ( $lat == 0 || $long == 0 ) {
                            continue;
                        }

                        $property_ref  = $property->webRef;
                        $property_name = $property->title;
                        $description   = htmlspecialchars( strip_tags( $property->longDescription ) );
                        $url           = get_home_url() . '/properties/' . $property->webRef;
                        $beds          = $property->bedrooms;
                        $baths         = $property->bathrooms;
                        $price         = $property->price . ' EUR';

                        //Gather Address Data
                        $addr1         = '';
                        $locality_name = $property->locality_details->localityName;

                        if ( strpos( $locality_name, ', ' ) !== false ) {
                            $locality = explode( ', ', $locality_name );
                            $city     = $locality[1];
                            $region   = $locality[0];
                            $country  = 'Italy';
                            if ( $locality[0] == 'Sicily' ) {
                                $country = $locality[0];
                            }
                        } else {
                            $city    = $region = $locality_name;
                            $country = $property->locality_details->zoneId == '3' ? 'Gozo' : 'Malta';
                        }

                        //Get property images
                        $property_images = [];


                        //Get property availability
                        $availability = 'for_sale';
                        if ( $property->marketType == 'LongLet' || $property->marketType == 'ShortLet' ) {
                            $availability = 'for_rent';
                        }

                        //Create listing child for this property
                        $listing = $listings->addChild( 'listing' );

                        //Get Property Type
                        $property_type_obj = $property->propertyType_details;
                        $property_type     = '';
                        if ( ! empty( $property_type_obj ) ) {
                            $property_type = $property_type_obj->description;
                        }

                        //Add Basic Data
                        $listing->addChild( 'home_listing_id', $property_ref );
                        $listing->addChild( 'name', htmlspecialchars( $property_name ) );
                        $listing->addChild( 'availability', $availability );
                        $listing->addChild( 'description', htmlspecialchars( strip_tags( $description ) ) );
                        $listing->addChild( 'url', $url );
                        $listing->addChild( 'latitude', $lat );
                        $listing->addChild( 'longitude', $long );
                        $listing->addChild( 'neighborhood', $city );
                        $listing->addChild( 'num_baths', $baths );
                        $listing->addChild( 'num_beds', $beds );
                        $listing->addChild( 'num_units', 1 );
                        $listing->addChild( 'price', $price );
                        $listing->addChild( 'property_type', '' ); //Not being used right now
                        $listing->addChild( 'listing_type', $availability . '_by_agent' );
                        $listing->addChild( 'property_typefs', $property_type );

                        //Add Address Data
                        $address = $listing->addChild( 'address' );
                        $address->addAttribute( 'format', 'simple' );
                        $address->addChild( 'component', $addr1 )->addAttribute( 'name', 'addr1' );
                        $address->addChild( 'component', $city )->addAttribute( 'name', 'city' );
                        $address->addChild( 'component', $region )->addAttribute( 'name', 'region' );
                        $address->addChild( 'component', $country )->addAttribute( 'name', 'country' );
                        $address->addChild( 'component', $country )->addAttribute( 'name', 'postal_code' );

                        //Add Image Data
                        $files = $property->files;
                        if ( ! empty( $files ) ) {
                            $images     = $listing->addChild( 'image' );
                            $file_count = 1;
                            foreach ( $files as $file ) {
                                if ( $file_count > 20 ) { //This is the max amount of images allowed on facebook XML
                                    break;
                                }
                                if ( stripos( $file->file_name, '.pdf' ) === false ) {
                                    $images->addChild( 'url', ApiController::get_api_image_url( $file->file_name ) );
                                }
                                $file_count ++;
                            }
                        }
                    }
                }
            }

            //Select the file to open based on the iteration and replace the content
            $file_to_open = $last_update ? $main_fb_xml_file : $temp_fb_xml_file;

            $fp = fopen( $file_to_open, 'w+' );
            fwrite( $fp, $listings->asXML() );
            fclose( $fp );

            write_log( 'FINISHED FACEBOOK ADS FEED UPDATE' );
        }


  • I would recommend the code from https://stackoverflow.com/a/1793240/1213708. This code would replace your code in just the part where you save the data (from `$fp = fopen( $file_to_open...` to the `fclose()` part). – Nigel Ren Oct 01 '20 at 15:54
  • Hi @NigelRen, thanks for your reply. I tried to replace the recommended code however the listing stopped from saving to the xml file :( ```//Select the file to open based on the iteration and replace the content $file_to_open = $last_update ? $main_fb_xml_file : $temp_fb_xml_file; $dom = new DOMDocument('1.0'); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->loadXML($listings->asXML()); echo $dom->saveXML(); write_log( 'FINISHED FACEBOOK ADS FEED UPDATE' );``` – mnppdt28 Oct 03 '20 at 18:05
  • It should be something like `$dom->save('fileName.xml');` instead of `echo $dom->saveXML();`. – Nigel Ren Oct 03 '20 at 18:17
  • hi @NigelRen do you think, the file is saved to $file_to_open? Should I make it ```$dom->save($file_to_open);``` instead? – mnppdt28 Oct 04 '20 at 08:30
  • It should be whatever file name you need it to be, that was just an example. – Nigel Ren Oct 04 '20 at 08:33

0 Answers0