I have this below code and it work fine
header ("content-type: text/xml");
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<urlset xmlns="http://www.google.com/schemas/sitepam/0.84">';
$xml .= '<url><loc>'.SiteRoot.'</loc><changefreq>daily</changefreq><priority>1.0</priority></url>';
$xml .= '<url><loc>'.SiteRoot.'/directory</loc><changefreq>daily</changefreq><priority>0.9</priority></url>';
$Query = mysql_query ("SELECT link FROM `om` ORDER BY `link`");
while($row = mysql_fetch_array($Query)) {
$xml .= '<url>';
$xml .= '<loc>'.GenerateLink( 'link',$row['link'] ).'</loc>';
$xml .= '<changefreq>weekly</changefreq>';
$xml .= '<priority>0.8</priority>';
$xml .= '</url>';
}
$xml .= '</urlset>';
echo $xml;
When i try to compress it with mime header
header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
Browser download a .gz file but it's not open. winrar give me a error that said: The archive is either in unknown format or damaged
This is the final code :
// header ("content-type: text/xml");
header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<urlset xmlns="http://www.google.com/schemas/sitepam/0.84">';
$xml .= '<url><loc>'.SiteRoot.'</loc><changefreq>daily</changefreq><priority>1.0</priority></url>';
$xml .= '<url><loc>'.SiteRoot.'/directory</loc><changefreq>daily</changefreq><priority>0.9</priority></url>';
$Query = mysql_query ("SELECT link FROM `om` ORDER BY `link`");
while($row = mysql_fetch_array($Query)) {
$xml .= '<url>';
$xml .= '<loc>'.GenerateLink( 'link',$row['link'] ).'</loc>';
$xml .= '<changefreq>weekly</changefreq>';
$xml .= '<priority>0.8</priority>';
$xml .= '</url>';
}
$xml .= '</urlset>';
echo $xml;
Try using some of the built in gzip functions like gzencode
echo gzencode($xml);
Related
I am trying to convert the sitemap.xml into sitemap.xml.gz , The pages I am extracting from the database , it is converting into sitemap.xml , but when I am trying to compress this is not working . this is my code it is making the file damage .
header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>';
$xmlString .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
include("admin/functions/dbconfig.php");
$sql = "select * from zz where aa between 1 and 30000";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($result)){
$url = $row["aa"];
$xmlString .= '<url>';
$xmlString .= '<loc>http://mynewdomain.com/page.php?word='.htmlentities($url).'</loc>';
$xmlString .= '<lastmod>'.date("Y-m-d").'</lastmod>';
$xmlString .= '<changefreq>monthly</changefreq>';
$xmlString .= '<priority>0.5</priority>';
$xmlString .= '</url>';
}
$xmlString .= '</urlset>';
gzwrite("compress", gzencode($xmlString));
gzclose("compress");
I am trying to create XML files via PHP for Googles Merchant center.
I had previously done the same with creating a sitemap but now am having issues.
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>';
$xmlString .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
$xmlString .= '<url>';
$xmlString .= '<loc>example.com</loc>';
$xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
$xmlString .= '<changefreq>daily</changefreq>';
$xmlString .= '<priority>1.0</priority>';
$xmlString .= '</url>';
$xmlString .= '</urlset>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);
$dom->save('../sitemap.xml');
This is more or less what I have to create my sitemap except I am obviously creating more URLs by querying my database.
But then I do more or less the exact same thing for my product feed but it does not work.
$xmlString .= '<?xml version="1.0" encoding="UTF-8"?>';
$xmlString .= '<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">';
$xmlString .= '<channel>';
$xmlString .= '<title>Product Feed</title>';
$xmlString .= '<link>https://www.example.com/</link>';
$xmlString .= '<description>Product Feed</description>';
$xmlString .= '<item>';
$xmlString .= '<g:id>'.$product_id.'</g:id>';
$xmlString .= '<title>'.$product_name.'</title>';
$xmlString .= '<description>'.$product_description.'</description>';
$xmlString .= '<link>https://www.example.com/product/'.$product_url.'</link>';
$xmlString .= '<g:condition>new</g:condition>';
$xmlString .= '<g:price>'.$rounded_lowest_sale_price.'</g:price>';
$xmlString .= '<g:availability>in stock</g:availability>';
$xmlString .= '<g:image_link>https://www.example.com/media/product_images/'.$first_image.'</g:image_link>';
$xmlString .= '<g:mpn>'.$model_no.'</g:mpn>';
$xmlString .= '<g:brand>'.$brand.'</g:brand>';
$xmlString .= '<g:google_product_category>Business & Industrial > Material Handling</g:google_product_category>';
$xmlString .= '</item>';
}
$xmlString .= '</channel>';
$xmlString .= '</rss>';
echo $xmlString;
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);
$dom->save('../product_feeds/product_feed_int.xml');
It saves the XML file but all it contains is:
<?xml version="1.0"?>
without even the UTF-8 encoding.
If you don't use DOMDocument to create the xml you can put your string directly into a file
file_put_contents('../product_feeds/product_feed_int.xml', $xmlString);
consider to use & for & in
Business & Industrial
This might Help
<?php
$xmldoc = new DOMDocument();
$xmldoc->encoding = 'utf-8';
$xmldoc->xmlVersion = '1.0';
$xmldoc->formatOutput = true;
$xml_file_name = 'xmlfile.xml';
$root = $xmldoc->createElement('Cars');
$car_node = $xmldoc->createElement('car');
$attr_movie_id = new DOMAttr('car_model', 'ritz');
$car_node->setAttributeNode($attr_movie_id);
$child_node_title = $xmldoc->createElement('Make', 'Maruti');
$car_node->appendChild($child_node_title);
$child_node_year = $xmldoc->createElement('Year', 2012);
$car_node->appendChild($child_node_year);
$child_node_genre = $xmldoc->createElement('Type', 'Hatchback');
$car_node->appendChild($child_node_genre);
$child_node_ratings = $xmldoc->createElement('Ratings', 6.2);
$car_node->appendChild($child_node_ratings);
$root->appendChild($car_node);
$xmldoc->appendChild($root);
$xmldoc->save($xml_file_name);
echo "$xml_file_name created";
?>
I am developing a webservice using Nusoap to send xml data, and this time I have to send data with latin characters like 'ó'. However when I put it in the soap client it stops working. Below is a summary of code being develped to test sending xml with latin characters.
This is ths summary of server code being developed:
include_once("nusoap/nusoap.php");
$server = new soap_server();
$server->configureWSDL("PersonImport","urn:PersonImport");
$server->register("PersonImport",array("login" => "xsd:string", 'senha' => 'xsd:string', 'fornecedor' => 'xsd:string'),array("return" => "xsd:string"),"urn:PersonImport","urn:PersonImport#PersonImport");
function PersonImport($login,$senha,$fornecedor) {
//Just for debug purposes
$return = "My login Is <b>".$login . "</b> And My senha Is <b>".$senha."</b> And My fornecedor Is <b>".$fornecedor."</b>.";
(...)(ommited code, xml parsing and response xml generation)
return $return;
}
This is ths summary of client code:
<?php
require_once("nusoap/nusoap.php");
$client = new soapclient("example.com?wsdl");
$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
$xml .= "<fornecedor>";
$xml .= "<NAME1>Gian</NAME1>";
$xml .= "<MCOD1>Giancarlo SA</MCOD1>";
$xml .= "<STCD1>80048303000113</STCD1>";
$xml .= "<STCD2>55670185501</STCD2>";
$xml .= "<STCD3>5508150087</STCD3>";
$xml .= "<RG>359730553</RG>";
$xml .= "<STRAS>rua itororó</STRAS>";
$xml .= "<HOUSE_NUM1>81</HOUSE_NUM1>";
$xml .= "<HOUSE_NUM2>301</HOUSE_NUM2>";
$xml .= "<ORT02>Menino Deus</ORT02>";
$xml .= "<PSTLZ>90110290</PSTLZ>";
$xml .= "<REGIO>RS</REGIO>";
$xml .= "<ORT01>Porto Alegre</ORT01>";
$xml .= "<TELF1>32335675</TELF1>";
$xml .= "<TELFX>32335675</TELFX>";
$xml .= "<SMTP_ADDR>teste#teste.com</SMTP_ADDR>";
$xml .= "<ERDAT>2016-10-04</ERDAT>";
$xml .= "<ChangeData>2016-10-04</ChangeData>";
$xml .= "<StartData>2016-10-04</StartData>";
$xml .= "<OffData>2016-10-04</OffData>";
$xml .= "</fornecedor>";
$result = $client->PersonImport("login","password", $xml);
echo $result;
The line
$xml .= "<STRAS>rua itororó</STRAS>";
has a special character. If I remove the 'ó' character it works.
I tried to set encoding on xml:
$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
This worked for me when I had to parse xml with SimpleXML Parser, but it didn't work on soap.
I tried to set header of the page for utf8 or ISO-8859-1 like this:
header("Content-type:text/html; charset=UTF-8");
or:
header ('Content-type: text/html; charset=ISO-8859-1');
I tried to use htmlentities, but the entity for 'ó' is '& o a c u t e;' which has the special character '&' and then the same problem happens.
function serialize didn't resolve the problem.
I couldn't find an answer until now on google.
Is it possible to pass latin special characters using nusoap? There must be a way.
Looks like I found a way. Using CDATA I can escape the '&', so I can use htmlentities on strings, like this:
$xml .= "<STRAS><![CDATA[".htmlentities("Rua Itororó")."]]></STRAS>";
I have tried to decode html tags in xml but the functions of the html tags was not worked.
Here is my code:
<?php
$sample = '<p>  Sample</p>
<p>Sample 2</p>';
header('Content-type: text/xml');
$output = '<rss version="2.0">';
$output .= '<channel>';
$output = '<description>.utf8_encode(html_entity_decode($sample)).</description>';
echo($output);
$output .= '</channel>';
$output .= '</rss>';
?>
The output was a plain text. The function of <p> tag was not working. and when I remove utf8_encode it error the .
Try this
<?php
$sample = '<p>Sample</p><p>Sample 2</p>';
header('Content-type: text/xml');
$output = '<rss version="2.0">';
$output .= '<channel>';
$output .= '<description>'. strip_tags(utf8_encode(html_entity_decode($sample))).'</description>';
$output .= '</channel>';
$output .= '</rss>';
echo($output);
Result:
<rss version="2.0">
<channel>
<description>
<p>Sample</p>
<p>Sample 2</p>
</description>
</channel>
</rss>
you need to seperate PHP function on string.
http://seosailor.com/beta2/articles/showrss this url give error Extra content at the end of the document on chrome
my code is this
function showrss() {
header("Content-Type: application/xml; charset=ISO-8859-1");
$query_items = "select * from articles";
$result_items = mysql_query ($query_items) or die("Some error: ".mysql_error());
$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?><rss version="2.0"><channel>';
while($row = mysql_fetch_array($result_items))
{
//$des = mysql_real_escape_string($row['a_description']);
// $a_des = str_replace(']]>', ']]>',$row['a_description']);
//$a_des = strip_tags($row['a_description']);
// $a_des = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags($row['a_description']));
$a_des = htmlspecialchars($row['a_description']);
$xml .= '<item>
<title>'.$row["a_title"].'</title>
<link>'.$row["a_url"].'</link>
<description>'.$a_des.'</description></item>';
} $xml .= '</channel>';
$xml .= '</rss></xml>';
echo $xml;}
That's because of the extra </xml> closing tag at the end (there is no opening <xml> tag).