php - 将 SimpleXMLElement 更改为漂亮的打印
问题描述
我们在 Facebook 目录上上传 XML 文件时遇到了这个问题。他们不再接受该文件,因为 XML 提要是在第 2 行生成的,而 Facebook 的每行限制为 5242880 字节。[当前 XML 文件][1] [1]:https://i.stack.imgur.com/ETqNh.png
这似乎是因为在我们的 php 中我们使用的是 SimpleXMLElement。请参阅下面的代码。但是,我们在 PHP 方面并不是那么好。我们如何更改功能,以便在生成和下载提要时,列表将显示为多行?
虽然,我从这个PHP SimpleXML 新行中找到了一个可能的解决方案,但我们是否应该只将字符串 SimpleXMLElement 替换为 DOMDocument 并插入其他代码?
谢谢,
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' );
}
解决方案
推荐阅读
- php - 如何使用 AJAX 在不同的 HTML 元素中显示 mysql 数据
- node.js - 环回 4 缓存与 redis
- python - 如何在 python 上制作镜面光照来实现 Phong 光照?
- javascript - 无法获取 __dirname 值
- javascript - 从多个表单字段或节点js中的多个输入类型上传多个图像
- python - 使用 pandas 从动态列中读取 Excel 数据
- python - Python中的内存错误或空返回列表
- swiftui - 如何在 SwiftUI 列表行中具有相同的比例?
- c++ - 如何在 C++ 中找到数组的确切大小?
- reactjs - 如何将 ts-nameof 与 create-react-app 或 craco 一起使用?