I hope you can put me in the right direction here.
I am busy pulling some images from my website to create an 3D image without downloading the images using a script.
The problem is the "sid" It doesn't seem to retrieve it through my php. The weird thing is if I paste the link into my browser I can see the json information and copy the "sid" into my php and it works. Problem is the session expires.
Currently I need it to retrieve the "sid" when I load my website page.
If you have any information regards to how I can do this, it would really be helpful.
Here is a snippet of what I am talking about:
Code: Select all
<?php
$sid = "I NEED TO GET THE SID";
$url ="https://hostname:port/cgi-bin/filemanager/utilRequest.cgi?func=download&sid=" . $sid . "&isfolder=0&compress=0&source_path=/folder1/images/360/&source_file=1_";
?>
<script>
//...... more code
for (var i = 1; i <= totalImages; i++) {
var imageUrl = '<?php echo $url; ?>' + i + '.png&source_total=1';
loadImage(imageUrl);
}
//....more code
</script>
I have tried this code to retrieve the SID:
Code: Select all
<?php
class Qnap {
public $sid;
private $loggedIn;
public function __construct($host, $user, $passwd, $port) { // Set default port to 8082
$this->sid = null;
$this->loggedIn = $this->login($host, $port, $user, $passwd);
}
// Add a public method to access $loggedIn
public function isLoggedIn() {
return $this->loggedIn;
}
private function login($host, $port, $user, $passwd) {
$loginEndpoint = "https://{$host}:{$port}/cgi-bin/authLogin.cgi?" . http_build_query([
'user' => str_replace('\\', '+', $user),
'pwd' => $passwd
]);
$xmlResponse = $this->req($loginEndpoint, true);
if ($xmlResponse !== null) {
$xml = simplexml_load_string($xmlResponse);
if ($xml === false) {
echo 'Failed loading XML';
foreach(libxml_get_errors() as $error) {
echo "\n", $error->message;
}
return false;
}
if (isset($xml->authPassed) && $xml->authPassed->__toString() === '1') {
$this->sid = $xml->authSid->__toString();
return true;
}
}
return false;
}
private function req($endpoint, $isXml = false) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
echo 'cURL Error: ' . curl_error($ch);
}
curl_close($ch);
return $isXml ? $response : json_decode($response, true);
}
}
// Example usage
$qnap = new Qnap('hostaddress', 'username', 'Password already encoded', 'port');
if ($qnap->loggedIn) {
echo 'SID: ' . $qnap->sid;
} else {
echo 'Login failed.';
}
?>
Many thanks all.
Regards,
Dewald Potgieter