2017-01-24 2 views
0

PHP를 사용하여 XML 사이트 맵을 동적으로 생성하고 있습니다. 생성 부분이 원활하게 실행되지만 결과를 XML 파일로 저장할 수 없습니다.PHP를 사용하여 동적으로 생성 된 XML 사이트 맵을 저장할 수 없습니다.

$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?><urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"/>'); 
$node = $xml->addChild('url'); 
$node->addChild('loc', 'http://www.pb.com/blog/'); 
$node->addChild('changefreq', "daily"); 
$node->addChild('priority', "1"); 
$connect = dbconn(PROJHOST, POSTSDB, POSTSUSR, POSTSPWD); 
$sql = "SELECT * FROM tblposts ORDER BY id"; 
$query = $connect->prepare($sql); 
if($query->execute()) { 
    $rows = $query->fetchAll(PDO::FETCH_ASSOC); 
    if($rows){ 
     foreach($rows as $row){ 
      $post_date = $row['post_date']; 
      $node = $xml->addChild('url'); 
      $node->addChild('loc', trim("http://www.pb.com/blog/" . $row['post_name'])); 
      $node->addChild('lastmod', str_replace(' ', 'T', $row['post_date']) . "-05:00"); 
      $node->addChild('changefreq', "weekly"); 
      $node->addChild('priority', "0.6"); 
     } 
    } 
} 
Header('Content-type: text/xml'); 
print($xml->asXML()); 

// $dom->preserveWhiteSpace = FALSE; 
$xml->save('sitemap.xml'); 

가 던지는 년대 치명적인 오류은 다음과 같습니다 : 여기에 내가 쓴 PHP의

전화 정의되지 않은 메서드 SimpleXMLElement로는 :: /xxx/xxx/xxx/sandboxgenerate_sitemap.php()에서 저장 on line 79

어떤 대안?

+0

이 같은 probelms을 얻을 때 ___ [Flippin Manual을 읽으십시오. 그곳에있는 곳] (http://php.net/manual/en/book.simplexml.php) ___ 저장 방법은 어디에 있습니까? Nowhere – RiggsFolly

+0

와우. 변명은하지 않았지만 무례 함은 정말로 보장되지 않았습니다. 그래도 도움을 주셔서 감사합니다. – TheLearner

+0

PHP에서'file_put_contents()'함수를 사용하여 파일에 쓸 수 있습니다. – mike510a

답변

1

SimpleXMLElement 클래스에는 save()라는 메서드가 없습니다. 위의 줄 에서처럼 asXML()을 사용하고 선택적 $ filename 매개 변수를 전달하십시오. 이 매개 변수를 지정하면 XML이 표시되지 않고 파일에 저장됩니다. 추가 문서는 여기에 있습니다 : http://php.net/manual/en/simplexmlelement.asxml.php