Sunday, 22 May 2016

Alfresco—Accessing repository using Restful API using PHP

Steps:

•Get a ticket using Alfresco login webscript (Restful API)
•Remove the xml tags present in the ticket
•Append the ticket to the url of required repository
•Append the server and port to the href array returned inorder to access the folders inside the repository


Code:

<!DOCTYPE html>
<html>

<body>

<h1>Company</h1>
<h1>Alfresco Repository</h1>

<?php
//get a ticket
$content=file_get_contents('http://<your server>:<your port>/alfresco/service/api/login?u=admin&pw=admin');
//echo $content."<br>";
$url="http://<your server>:<your port>/alfresco/service/sample/folder/Company%20Home";
//echo "Hello World!<br>";
$a="?alf_ticket=";
//echo $a."<br>";
$b=$content;

//echo $b."<br>";
//remove the xml tags
$b= strip_tags($b);
//echo $b."<br>";
//echo $content."<br>";
//append the ticket to the url of required repository
$url1=$url.$a.$b;
//remove the spaces if present
$url1=(string)strip_tags($url1);
//echo $url1."<br>";

//$url2="http://<your server>:<your port>/alfresco/service/api/node/content/workspace/SpacesStore/0f4fe3cf-5eaa-4014-9357-b6f16a1b4b61/strt.png?alf_ticket=TICKET_2fa45f2c268d98a7b215fff83b96f2c9baf4fa88";
convert the url into a srting
$url1=(string)$url1;
//$url2=(string)$url2;
//echo var_dump($url1);
//echo var_dump($url2);
$aa="aa";
$bb="aa";
//remove all types of spaces in the url
$url1 = str_replace(' ', '', $url1);
$url1 = preg_replace('/\s+/', '', $url1);
//if($url1==$url2)
//echo "equal<br>";
//echo $url2."<br>";
//echo $url1."<br>";
//echo gettype($url1);

//get the contents of url
echo file_get_contents($url1);
//echo "<img src=".$url2.">";


/* $opts = array('http' =>
    array(
        'method'  => 'GET',
        //'user_agent '  => "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2) Gecko/20100301 Ubuntu/9.10 (karmic) Firefox/3.6",
        'header' => array(
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8
'
        ),
    )
);
$context  = stream_context_create($opts);

$f = file_get_contents($url1, false, $context);
echo $f; */


?>
<script>


//the contents of the url have the href from the current repository. so we need to append the server and port to the href inorder to access the folders inside the repository
//get the size of the array of the elements having href
var a = document.getElementsByTagName("table")[0].getElementsByTagName("a")
 length = a.length;
// alert(a+length);


 for(var i=0; i< length; i++){
 var g=document.getElementsByTagName("table")[0].getElementsByTagName("a")[i].getAttribute("href");
//get all the href elements and append the server address
  document.getElementsByTagName("table")[0].getElementsByTagName("a")[i].setAttribute("href","http://<your server>:<your port>"+g);

}


document.getElementsByTagName("table")[0].getElementsByTagName("a")[0].setAttribute("href","http://<your server>:<your port>"+g);
//alert(document.getElementsByTagName("table")[0].getElementsByTagName("a")[0].getAttribute("href"));
</script>
</body>

No comments:

Post a Comment