Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在codeigniter php中使用cURL保持会话_Php_Session_Curl_Codeigniter 3 - Fatal编程技术网

在codeigniter php中使用cURL保持会话

在codeigniter php中使用cURL保持会话,php,session,curl,codeigniter-3,Php,Session,Curl,Codeigniter 3,我有一个外部API,我想在其中获取一些数据,我想在所有请求中保留会话id,直到我注销。在codeigniter中使用cURL lib,我有以下流程(myacc和mypass只是占位符): 这将输出: {"data":{"sid":"lH6WJCWMm5rkA14B0MPN570354"},"success":true} 在发出下一个请求时,我必须保留提供的sid(会话id): http://37.99.110.537:6001/webapi/entry.cgi?api=SYNO.Surveil

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

这将输出:

{"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),然后注销


我认为在我的案例中不需要保存和使用文件中的cookies,我认为类似于:

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保持在请求链上?

如果您想保持
sid
长会话,对于多个请求等,您可以将此
json
保存到一些
json
文件中,并在注销时清除文件内容

function logout()
{
    file_put_contents('path/to/file', json_encode(['logout' => 'true']));
}
$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
,以便在下一次执行时生成并使用新的。

“我认为保存和使用cookie”您是否需要
sid
将其附加到
url
或通过cookie发送?只是将其附加到url。您能告诉我您使用的是哪个库或源吗?我也有同样的问题?我需要图书馆!您好@JayminsFakeAccount,我正在使用:您只需下载库并将其放入您的库文件夹中,您就可以使用它了,如果您有任何问题,请告诉我,我还找到了一种将sid与其他cookie一起存储的好方法!