简体   繁体   中英

Keep session with cURL in codeigniter php

I have a external API where I want to GET some data, and I want to keep session id through all the request until I log out. Using cURL lib in codeigniter I have the following flow (myacc and mypass are just placeholders):

public function getCURL() {
   echo $this->curl->simple_get('http://37.99.110.537:6001/webapi/auth.cgi?api=SYNO.API.Auth&method=login&version=2&account=myacc&passwd=mypassD&format=sid&session=SurveillanceStation');
}

This will output:

{"data":{"sid":"lH6WJCWMm5rkA14B0MPN570354"},"success":true}

I will have to keep that provided sid (session id) when making the next request:

http://37.99.110.537:6001/webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera&method=GetSnapshot&version=1&cameraId=2&timestamp=1480512959&preview=true&_sid="lH6WJCWMm5rkA14B0MPN570354"

See at the end sid="lH6WJCWMm5rkA14B0MPN570354" .

And then log out and kill that sid.

After each login I would get a new sid that I have to use it to get a picture (with that URL) and then logout.

I think that saving and using cookies from a file in my case isn't needed, I think something like:

public function getCURL() {
   echo $this->curl->simple_get('http://37.99.210.237:6001/webapi/auth.cgi?api=SYNO.API.Auth&method=login&version=2&account=myacc&passwd=mypassD&format=sid&session=SurveillanceStation');

   if ($this->form_validation->run()){
            $data= array(
                'sid'=> $this->input->post('sid'),
                'is_logged_in' => true
            );
$this->session->set_userdata($data);

if(false == $this->CI->session->userdata('is_logged_in')) {
       echo $this->curl->simple_get('http://37.99.110.537:6001/webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera&method=GetSnapshot&version=1&cameraId=2&timestamp=1480512959&preview=true&_sid="sid"');

}

}
}

^^ That syntax is messed up, but how I can make it in a proper way or how it's the best way to keep session id on the request chain ?

if you want to keep sid for long session, for multiple request etc, you can save this json to some json file and clear content of file while logging out.

wrap your $sid getter to some other function.

function getSid()
{
    //try to read from json
    if(is_file('path/to/sid.json'){
        $sid = json_decode(file_get_contents('path/to/sid.json', true));
        if(!isset($sid['logout'])){
            return $sid['data']['sid'];
        }
    }
    $sid = $this->curl->simple_get('http://37.99.110.537:6001/webapi/auth.cgi?api=SYNO.API.Auth&method=login&version=2&account=myacc&passwd=mypassD&format=sid&session=SurveillanceStation');

    //check and save `$sid`

    if(strlen($sid) > 20) {
        file_put_contents('path/to/sid.json', $sid);
        return json_decode($sid, true)['data']['sid'];
    }
    return false;
}

and update content of sid.json while logging out.

function logout()
{
    file_put_contents('path/to/file', json_encode(['logout' => 'true']));
}

and call these methods.

for every request in one execution, it will use the same sid , and when you'll hit 'logout()' it will destroy the sid so that new generated and used on next execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM