2009-03-21 12 views
1

私はPHPでRSSフィードを構築するための機能を持っている:RSSは機能

function createRSS() { 
    $currentDate = time(); 
    $sql = "SELECT * FROM ". ADS_TABLE ." WHERE expires > $currentDate ORDER BY enterd DESC LIMIT 0,400"; 
    $results = myExec($sql); 
    // open a file pointer to an RSS file 
    $fp = fopen ("mexautosrss.xml", "w"); 
    if (!$fp) { 
    // Can't write to a file 
    return; 
    } 

    fwrite ($fp, "<?xml version='1.0' encoding='iso-8859-1' ?>\n"); 
    //ERROR: fwrite ($fp, "<rss version='2.0' xmlns:atom="http://www.mexautos.com/mexautosrss.xml"><channel>\n"); 
    fwrite ($fp, "<title>MexAutos.com</title>\n"); 
    //ERROR: fwrite ($fp, "<atom:link href="http://www.mexautos.com/mexautosrss.xml" rel="self" type="application/rss+xml" />\n"); 
    fwrite ($fp, "<link>http://www.mexautos.com/</link>\n"); 
    fwrite ($fp, "<description>Anuncios de Autos y Camionetas Usados en Mexico.</description>\n"); 
    fwrite ($fp, "<language>es-mx</language>\n"); 
    fwrite ($fp, "<docs>http://www.mexautos.com/mexautosrss.xml</docs>\n"); 
    fwrite ($fp, "<image>\n"); 
    fwrite ($fp, " <title>Mexautos.com</title>\n"); 
    fwrite ($fp, " <url>http://www.mexautos.com/images/logo_top_es.gif</url>\n"); 
    fwrite ($fp, " <link>http://www.mexautos.com</link>\n"); 
    fwrite ($fp, "</image>\n"); 

    while ($row = mysql_fetch_array($results)) { 
    $makeId = $row['make']; 
    $makeSQL = "SELECT name FROM ". CAR_MAKES_TABLE ." WHERE pkMakeID=$makeId"; 
    $makeResults = myExec($makeSQL); 
    $make = mysql_fetch_row($makeResults); 
    $modelId = $row['model']; 
    $makeSQL = "SELECT name FROM ". CAR_MODELS_TABLE ." WHERE pkModelID=$modelId"; 
    $makeResults = myExec($makeSQL); 
    $model = mysql_fetch_row($makeResults); 
    $stateId = $row['state']; 
    $makeSQL = "SELECT name FROM ". STATES_TABLE ." WHERE pkStateID=$stateId"; 
    $makeResults = myExec($makeSQL); 
    $state = mysql_fetch_row($makeResults); 
    $title = $make[0]." ".$row['make_other']." ".$model[0]." ".$row['model_other']." '".$row['model_year']; 
    $content = "$".$row['price']." mil pesos ".$row['color']." Clima: ".$row['clima']." ".$row['milage']." mil kms Puertas: ".$row['doors']." ".$row['transmission']." ".$row['comment']." Tel: ".$row['tel_num']." (".$state[0].")"; 
    $search = array(
    '@<script[^>]*?>.*?</script>@si', // Strip out javascript 
    '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags 
    '@([\r\n])[\s][email protected]', // Strip out white space 
    '@&(quot|#34);@i', // Replace HTML entities 
    '@&(amp|#38);@i', 
    '@&(lt|#60);@i', 
    '@&(gt|#62);@i', 
    '@&(nbsp|#160);@i', 
    '@&(iexcl|#161);@i', 
    '@&(cent|#162);@i', 
    '@&(pound|#163);@i', 
    '@&(copy|#169);@i', 
    '@&(acento|#0027);@i', 
    '@&#(\d+);@e'); // evaluate as php 
    $replace = array(
    '', 
    '', 
    '\1', 
    '"', 
    '&', 
    '<', 
    '>', 
    ' ', 
    'chr(161)', 
    'chr(162)', 
    'chr(163)', 
    'chr(169)', 
    'chr(\1)'); 
    $content = preg_replace($search, $replace, $content); 
    $title = preg_replace("/&/", 'y', $title); 
    $content = preg_replace("/&/", 'y', $content); 
    fwrite ($fp, "<item>\n"); 
    fwrite ($fp, " <title>$title</title>\n"); 
    fwrite ($fp, " <description>$content</description>\n"); 
    fwrite ($fp, " <link>http://www.mexautos.com/</link>\n"); 
    fwrite ($fp, "<guid>http://www.mexautos.com</guid>\n"); 
    fwrite ($fp, "</item>\n"); 
    } 
    fwrite ($fp, "</channel></rss>\n"); 
    fclose ($fp); 
    } 

問題は、私は「アトム:リンク」コメントアウトしていない場合、私はエラーを取得することである行が、それは示しています

解析エラー:構文エラー、予期しないT_STRING

は、誰かがどこにそのエラー私を指すもらえますか?

Thxを

答えて

1

あなたがPHPで二重引用符で囲まれた文字列の中(あるいは単に他の言語についての)二重引用符が含まれている場合は、それらのリテラルの二重引用符をエスケープする必要があります。

fwrite ($fp, "<rss version='2.0' xmlns:atom=\"http://www.mexautos.com/mexautosrss.xml\"><channel>\n"); 

さもHTMLで一般的に互換性があり、単一引用符を使用する: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2

By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa.

を:

fwrite ($fp, "<rss version='2.0' xmlns:atom='http://www.mexautos.com/mexautosrss.xml'><channel>\n"); 

は、HTML/SGMLは、ここに属性を区切るために引用種類の仕様を参照してください。最後の文は、あなたのシナリオではない引用符を含むHTML属性文字列に適用されます。しかし引用符を含む引用符付きの文字列を持つPHPコードがあり、PHPは引用符について同じポリシーをサポートしています。あなたは正しく文字列をエスケープする必要が

+0

実はこれはXMLではなくHTML/SGMLですが、同じことが当てはまる:XMLは、引用符のいずれかの種類が属性を区切ることができます。 –

1

fwrite ($fp, "<rss version='2.0' xmlns:atom=\"http://www.mexautos.com/mexautosrss.xml\"><channel>\n");