Want to show XML view in html using php

197 Views Asked by At

I have oai.php page.Now when i type in url oai.php?verb=identify then want to show the contents in identify.xml.But i want the same url(oai.php?verb=identify) when i view the identify.xml.Below code showing only data of xml.

 <?php

include "config.php";
$id= $_GET['verb'];
//echo $id;
if($id=="identify")
{
    $date_mod = gmdate("Y-m-d\TH:i:s\Z");
    $rss_txt ='<?xml version="1.0" encoding="utf-8"?>
    <OAI-PMH xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.openarchives.org/OAI/2.0/" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
    <responseDate>' .$date_mod. '</responseDate>
    <request verb="Identify">http://mywebsite.org/oai</request>
     <Identify>

            <repositoryName>mydomain</repositoryName>
            <baseURL>mywesite.com</baseURL>
            <protocolVersion>2.0</protocolVersion>
            <adminEmail>myemail</adminEmail>
            <earliestDatestamp>'.$date_mod.'</earliestDatestamp>
            <deletedRecord>transient</deletedRecord>
            <granularity>YYYY-MM-DDThh:mm:ssZ</granularity>


     </Identify>
     </OAI-PMH>';
}
else if($id=="ListRecords")
{
    include "config.php";

    $date_mod = gmdate("Y-m-d\TH:i:s\Z");
    $viewss1=$con->query("SELECT COUNT(upload_paper_id) FROM upload_papers");
    $cview1=$viewss1->fetch_row();
    $countt1=$cview1[0];

    $rss_txt = '<?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="http://localhost/oai-file/data/oai2.xsl" ?>
    <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
    <responseDate>' .$date_mod. '</responseDate>
    <request verb="ListRecords" metadataPrefix="oai_dc">http:mywebsite.org</request>
    <ListRecords>';
        $query = "SELECT * FROM upload_papers";
        $result=$con->query($query);
        while($values_query = mysqli_fetch_assoc($result))
        {
            $year= $values_query['year'];
            $upload_paper_id= $values_query['upload_paper_id'];
    $rss_txt = '<record>
    <header>

    <identifier>' .$upload_paper_id. '</identifier>
    <datestamp>' .$date_mod. '</datestamp>
    <setSpec>mydoc8:BIH</setSpec>

    </header>
    <metadata>



    <oai_dc:dc
        xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/
        http://www.openarchives.org/OAI/2.0/oai_dc.xsd">    
    <dc:title xml:lang="en-US">' .$values_query['paper_title']. '</dc:title>
    <dc:creator>' . $values_query['author_name']. '</dc:creator>    
    <dc:creator>' . $values_query['co_author_name']. '</dc:creator> 
    <dc:description xml:lang="en-US">' .$values_query['abstract']. '</dc:description>   
    <dc:publisher xml:lang="en-US">' .$values_query['journal_nam']. '</dc:publisher>    
    <dc:contributor xml:lang="en-US">' .$values_query['author_name']. '</dc:contributor>
    <dc:type xml:lang="en-US">Article</dc:type> 
    <dc:format>application/pdf</dc:format>  
    <dc:identifier>http://mywebsite'.$year.'/article.php?page='.$upload_paper_id.'</dc:identifier>
    <dc:language>en</dc:language>   
    </oai_dc:dc>
    </metadata>
            </record>';
     }  
               $rss_txt = '<resumptionToken
                completeListSize="'.$countt1.'"
                cursor="0">'.$date_mod.'</resumptionToken>


    </ListRecords>
    </OAI-PMH>';


}
else if($id=="ListSets")
{
    $rss_txt =file_get_contents("ListSets.xml");
}
header('Content-type: application/xml');
echo $rss_txt;

?>

Above code shows only the data of xml.The output attached below Out put And i want the output likeWanted output .If i type verb=identify in url then it want to show identify.xsd or xml file any one.Then i type Listsets Want to show the content in that page.if verb equal to empty want to show the identify page.Please help me.Its really irritating me.Thank u

1

There are 1 best solutions below

6
On BEST ANSWER

There are a few problems with the code. The main thing is to set your header to be XML content type. This will tell the browser what to expect. But there are a few other issues.

$id= $_GET['verb'];
//echo $id;
if($id=="identify")
{
    $date_mod = gmdate("Y-m-d\TH:i:s\Z");
    $rss_txt ='<?xml version="1.0" encoding="utf-8"?>
    <OAI-PMH xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.openarchives.org/OAI/2.0/" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
    <responseDate>' .$date_mod. '</responseDate>
    <request verb="Identify">http://mywebsite.org/oai</request>
     <Identify>

            <repositoryName>mydomain</repositoryName>
            <baseURL>mywesite.com</baseURL>
            <protocolVersion>2.0</protocolVersion>
            <adminEmail>myemail</adminEmail>
            <earliestDatestamp>'.$date_mod.'</earliestDatestamp>
            <deletedRecord>transient</deletedRecord>
            <granularity>YYYY-MM-DDThh:mm:ssZ</granularity>


     </Identify>
     </OAI-PMH>';
}
else if($id=="ListRecords")
{
    $rss_txt =file_get_contents("compend1.xml");
}
else if($id=="ListSets")
{
    $rss_txt =file_get_contents("ListSets.xml");
}
header('Content-type: application/xml');
echo $rss_txt;

You had $rss_txt .= which with the . means append the string, which as you didn't declare it anywhere gives you the error about an undefined variable. Also with the content - XML needs to start right at the beginning of the string, so remove the whitespace.

The main one is having...

header('Content-type: application/xml');

But, for this to work properly, you need to ensure you comment out the echo, which I assume was some debugging code.

When doing an if - a single = is used to assign a value, so you need == to compare the values.

You can use $_GET[] to access the parameters rather than the way you were using, a lot simpler.

I've used file_get_contents() to fetch the data from those other files and moved the echo to the end so they all have the same logic.