【问题标题】:Keep session with cURL in codeigniter php在 codeigniter php 中与 cURL 保持会话
【发布时间】:2017-02-03 13:16:44
【问题描述】:

我有一个外部 API,我想在其中获取一些数据,并且我想在所有请求中保留会话 ID,直到我注销。在 codeigniter 中使用 cURL lib 我有以下流程(myacc 和 mypass 只是占位符):

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');
}

这将输出:

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

在发出下一个请求时,我必须保留提供的 sid(会话 ID):

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"

见最后sid="lH6WJCWMm5rkA14B0MPN570354"

然后注销并杀死该 sid。

每次登录后,我都会获得一个新的 sid,我必须使用它来获取图片(带有该 URL),然后注销。

我认为在我的情况下不需要保存和使用文件中的 cookie,我认为是这样的:

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"');

}

}
}

^^ 语法搞砸了,但是我怎样才能以正确的方式实现它,或者它是如何将会话 ID 保留在请求链上的最佳方式?

【问题讨论】:

  • “我认为保存和使用 cookie”
  • 您需要将sid 附加到url 还是通过cookie 发送?
  • 只是将其附加到 url。
  • 您能告诉我您使用的是哪个库或源吗?我有同样的问题?我需要卷曲库!
  • 您好@JayminsFakeAccount,我正在使用:github.com/philsturgeon/codeigniter-curl 您只需下载 lib 并将其放入您的 lib 文件夹即可使用它,如果您有任何问题,请告诉我,我还找到了一种将 sid 与其他 cookie 一起存储的好方法!

标签: php session curl codeigniter-3


【解决方案1】:

如果您想将sid 保留为长时间会话、多个请求等,您可以将此json 保存到某个json 文件中,并在注销时清除文件内容。

将您的 $sid getter 包装到其他函数。

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;
}

并在注销时更新sid.json 的内容。

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

并调用这些方法。

对于一次执行中的每个请求,它将使用相同的sid,当您点击“logout()”时,它将破坏sid,以便在下次执行时生成并使用新的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    相关资源
    最近更新 更多