2011-01-23 49 views
0

我有從我的數據庫中提取信息然後使用API​​檢查它的代碼。我試圖將返回信息自定義到表中。我會添加一張照片來顯示我想要的內容。我到處搜索,我不知道如何編碼。不要擔心CSS只是獲取信息來填充表格。以XML格式返回的樣式Feed內容

<?php 
     $key = "********************"; 
     $address = urlencode($CustomFields->field('jr_address',$listing,false,false)); 
     $city = $listing['Category']['title']; 
     $zip = $CustomFields->field('jr_zipcode',$listing,false,false); 
     $url = "http://api.greatschools.org/schools/nearby?key={$key}&address={$address}&city={$city}&state=MI&zip={$zip}&schoolType=public-charter&levelCode=elementary-schools&minimumSchools=50&radius=10&limit=5"; 
     $curl = curl_init(); 
     curl_setopt($curl, CURLOPT_URL, $url); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
     $response = curl_exec($curl);  
     curl_close($curl); 
     print_r($response); 
?><style type='text/css'>gsId{display:none;}name{color:#9BB055; font-size:18px;}type{display:none;}gradeRange{}enrollment{}gsRating{}city{display:none;}state{display:none;}district{}districtNCESId{display:none;}address{display:none;}phone{display:none; }fax{display:none;}ncesId{display:none;}lat{display:none;}lon{display:none;}</style> 

返回代碼:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<schools> 
<school> 
<gsId>6350</gsId> 
<name>Chinese Education Center</name> 
<type>public</type> 
<gradeRange>K-5</gradeRange> 
<enrollment>63</enrollment> 
<gsRating>3</gsRating> 
<city>San Francisco</city> 
<state>CA</state> 
<district>San Francisco Unified School District</district> 
<districtNCESId>0634410</districtNCESId> 
<address>657 Merchant St., San Francisco, CA 94111</address> 
<phone>(415) 291-7918</phone> 
<fax>(415) 291-7965</fax> 
<ncesId>063441005596</ncesId> 
<lat>37.795</lat> 
<lon>-122.4042</lon> 
<overviewLink>http://www.greatschools.org/california/san-francisco/6350-Chinese-Education-Center/?s_cid=gsapi</overviewLink> 
<ratingsLink>http://www.greatschools.org/school/rating.page?state=CA&amp;id=6350&amp;s_cid=gsapi</ratingsLink> 
<reviewsLink>http://www.greatschools.org/school/parentReviews.page?state=CA&amp;id=6350&amp;s_cid=gsapi</reviewsLink> 
</school> 
<school> 
<gsId>6389</gsId> 
<name>Gordon J. Lau Elementary School</name> 
<type>public</type> 
<gradeRange>K-5</gradeRange> 
<enrollment>667</enrollment> 
<gsRating>7</gsRating> 
<city>San Francisco</city> 
<state>CA</state> 
<district>San Francisco Unified School District</district> 
<districtNCESId>0634410</districtNCESId> 
<address>950 Clay St., San Francisco, CA 94108</address> 
<phone>(415) 291-7921</phone> 
<fax>(415) 291-7952</fax> 
<website>http://www.gjles.org/</website> 
<ncesId>063441005599</ncesId> 
<lat>37.794</lat> 
<lon>-122.4086</lon> 
<overviewLink>http://www.greatschools.org/california/san-francisco/6389-Gordon-J.-Lau-Elementary-School/?s_cid=gsapi</overviewLink> 
<ratingsLink>http://www.greatschools.org/school/rating.page?state=CA&amp;id=6389&amp;s_cid=gsapi</ratingsLink> 
<reviewsLink>http://www.greatschools.org/school/parentReviews.page?state=CA&amp;id=6389&amp;s_cid=gsapi</reviewsLink> 
</school> 
</schools> 

我不知道該怎麼寫腳本,使它看起來像這樣: 鏈接到圖片: link text


所以新文件看起來像這樣?

<?php 
    $key = "xxxxxxxxxxxxxxxxxxxxxxxx"; 
    $address = urlencode($CustomFields->field('jr_address',$listing,false,false)); 
    $city = $listing['Category']['title']; 
    $zip = $CustomFields->field('jr_zipcode',$listing,false,false); 
    $url = "http://api.greatschools.org/schools/nearby?key={$key}&address={$address}&city={$city}&state=MI&zip={$zip}&schoolType=public-charter&levelCode=elementary-schools&minimumSchools=50&radius=10&limit=5"; 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    $response = curl_exec($curl);  
    curl_close($curl); 
    echo (xml2html($response, "/xslt/schools.xsl"); 

?>

我得到迴音線的錯誤。

這是哪裏去的?

<?php 
function xml2html($xmldata, $xslPath) 
{ 
    /* $xmldata -> your XML */ 
    /* $xsl -> XSLT file */ 

    $arguments = array('/_xml' => $xmldata); 
    $xsltproc = xslt_create(); 
    xslt_set_encoding($xsltproc, 'ISO-8859-1'); 
    $html = 
     xslt_process($xsltproc, 'arg:/_xml', $xslPath, NULL, $arguments); 

    if (empty($html)) { 
     die('XSLT processing error: '. xslt_error($xsltproc)); 
    } 
    xslt_free($xsltproc); 
    return $html; 
} 
?> 
+0

也許有不止一個學校一個XML文檔將更有幫助。另外,每所學校都有經緯度,但是應該計算與學校距離的點的座標是什麼?這不是在你的問題中指定的。 – 2011-01-23 06:58:04

+0

我在上面的代碼中包含了另一所學校。就緯度和經度而言,我不知道你是否看過我發佈的圖像(在原始文章的底部)。我只會隱藏它。當我將地址信息發送給API時,它將排序哪些學校更接近。我的挑戰是我的知識是非常有限的,我迷失於如何編碼任何這些。 – 2011-01-23 16:56:52

回答

0

瀏覽器無法看到自定義XML(即您的學校xml)。 所以你應該手動將其轉換爲HTML。我知道2級的解決方案:

  1. 解析XML與xml parser functions
  2. 通過XSLT轉換板XML(對我來說,更好的辦法)。

對於XML提供了我寫的下一個XSLT:link text(對不起,外部鏈接,我在這裏粘貼XSLT代碼無法解釋的煩惱)

轉型可以由功能xslt_process應用。 (你應該在你的服務器的(例如「XSLT/schools.xsl」文件夾中存放的地方XSL文件)和轉換XML響應):

<?php 
function xml2html($xmldata, $xslPath) 
{ 
    /* $xmldata -> your XML */ 
    /* $xsl -> XSLT file */ 

    $arguments = array('/_xml' => $xmldata); 
    $xsltproc = xslt_create(); 
    xslt_set_encoding($xsltproc, 'ISO-8859-1'); 
    $html = 
     xslt_process($xsltproc, 'arg:/_xml', $xslPath, NULL, $arguments); 

    if (empty($html)) { 
     die('XSLT processing error: '. xslt_error($xsltproc)); 
    } 
    xslt_free($xsltproc); 
    return $html; 
} 

// making request... 
$response = curl_exec($curl);  
curl_close($curl); 
echo(xml2html($response, "/xslt/schools.xsl")); 

>