linslin / Yii2-Curl

:curly_loop: Yii2 Curl extension based on PHP cURL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Do not reinitialize curl resource. Make several request with the same session/cookies.

ODY90 opened this issue · comments

I need to login on site and make several request as logined user. But for each request method _httpRequest make curl_init($this->getUrl()), so for each request i am new user.
My code:

$curl
    ->setOption(CURLOPT_COOKIESESSION, 1)
    ->setOption(CURLOPT_COOKIEJAR, '/tmp/cookies.txt')
    ->setPostParams($loginParams)
    ->post(LOGIN_URL);
$curl->setPostParams($anotherParams)->post(ANOTHER_URL);

After first request i am loginned, all good. On second request i got error that i am not logined...

I porpose make some changes to _httpRequest method:
replace line 603
to

if ($this->curl === null) {
    $this->curl = curl_init();
}
curl_setopt($this->curl, CURLOPT_URL, $this->getUrl());

Thanks for your request. We gonna check this in the next weeks. We are currently stucking in other projects.

@ODY90 we checked this request carefully but we did not found any lacks inside the currently implemented logic. The options inside your instance will not get lost until you call reset(), unsetOptions() or unsetOption(). Your options:

$curl
    ->setOption(CURLOPT_COOKIESESSION, 1)
    ->setOption(CURLOPT_COOKIEJAR, '/tmp/cookies.txt')
    ->setPostParams($loginParams)
    ->post(LOGIN_URL);

//will keep the options you set before on that instance.
$curl->setPostParams($anotherParams)->post(ANOTHER_URL);

... will be set on each new request, even when curl_init() is initialized again on each request. Maybe we are wrong but by reffering this https://stackoverflow.com/questions/30760213/save-cookies-between-two-curl-requests you should be able to handle your cookie session by using CURLOPT_* yourself. If not, please create a full qualyfied example of what is not working.

Thank you
Lin

@ODY90 any feedback?

@ODY90 we going to close this issue. Once you provide some feedback we will reopen it.