CodeIgniter Forums
CurlRequest Cookies not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: CurlRequest Cookies not working (/showthread.php?tid=88969)



CurlRequest Cookies not working - Fred9176 - 12-12-2023

Hello,

I'm migrating an CI3 app to CI4 (4.4.3) and I have problem to make CurlRequest working fine with cookies.
In CI3, I used plain old PHP curl : 

PHP Code:
$ch curl_init();
curl_setopt($chCURLOPT_COOKIESESSIONTRUE);
curl_setopt($chCURLOPT_COOKIEJAR$cookieFile);
curl_setopt($chCURLOPT_COOKIEFILE$cookieFile);
curl_setopt($chCURLOPT_URL$loginUrl));
$content curl_exec($ch); 


This work fine and my cookie file looks like this : 

Code:
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

192.168.1.12    FALSE  /      FALSE  0      sid    12271



When I try to do the same in CI4 with the following code :

PHP Code:
$options = [
            'cookie' => $ookieFile,
];
$curlRequest = \Config\Services::curlrequest($options);
$response $curlRequest->get($oginUrl); 


No cookie is used and the cookie file is empty : 
Code:
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

It seems it's missing something like CURLOPT_COOKIESESSION 

Do you have any idea ?

Thanks,

Fred


RE: CurlRequest Cookies not working - kenjis - 12-12-2023

Cannot reproduce.


PHP Code:
    public function index()
    {
        $options = [
            'cookie' => WRITEPATH 'cookie.txt',
        ];
        $curlRequest = \Config\Services::curlrequest($options);
        $response $curlRequest->get('https://github.com/');
    



Code:
$ cat writable/cookie.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_.github.com TRUE / TRUE 1734041844 logged_in no
.github.com TRUE / TRUE 1734041844 _octo GH1.1.1820761395.1702419444
#HttpOnly_github.com FALSE / TRUE 0 _gh_sess IS3xd90GKt2Ggk8Lmvt7i2T3UCy5kScqxzOrA2qISp5tWRy6BPmcX577e1%2BgHM3VRx95FeAgvs5LxG5mYiuZvt%2FlpdjUQqVFCmzY7JoAqY8CGDs8MPIE5y%2B3fWY3ZO5ldAmZy3CtXK6QJA3yXVXvtZNZvwkYerg%2F48QV%2BJoItixdN2017cyfHPmk5PaA4SucW3LdaRweSdxCzWUMveila1K89UgaXrKNW1eViqCh40TYWW%2BRmwMrYEr26dkSZ%2BXzBlAcIUeVolS7Uw741GevdQ%3D%3D--M%2FEwJ7MVIYkpgmW5--ikiHp20yyGi2uHA%2BAUXZDQ%3D%3D



RE: CurlRequest Cookies not working - Fred9176 - 12-13-2023

Thank you,

I think I found where the problem is :
In fact : the login page redirects to an other page, which sets the cookie.

The problem is that this redirection changes from POST to GET.
By default (and I guess it was not the case is my previous CI 3 setup), curl redirect POST to POST and then I got a timeout
I disabled redirect as I don't need the second page.

I then tried to enable redirect from POST to GET using  following option :

PHP Code:
'allow_redirects' => [
    'strict'      => FALSE,
], 

When I look at the debug file, it seems that CI4/curl noticed to change from POST to GET, but finally do a POST request instead of a GET anyway: 

Code:
< HTTP/1.1 200 OK
< Connection: close
< Content-Type: text/html
< Cache-Control: no-cache
< Pragma: no-cache, no-store
<
* Closing connection
*  Trying 192.168.1.12:80...
* Connected to 192.168.1.12 (192.168.1.12) port 80
> POST /fr/login.htm HTTP/1.1
Host: 192.168.1.12
Accept: */*
Content-Length: 50
Content-Type: application/x-www-form-urlencoded

< HTTP/1.1 302 Found
< Connection: close
* Please rewind output before next send
< Location: /fr/welcome.htm
<
* Closing connection
* Issue another request to this URL: 'http://192.168.1.12/fr/welcome.htm'
* Switch from POST to GET                     <===== HERE, WE CAN SEE IT WILL TRY TO CHANGE POST TO GET
* Hostname 192.168.1.12 was found in DNS cache
*  Trying 192.168.1.12:80...
* Connected to 192.168.1.12 (192.168.1.12) port 80
> POST /fr/welcome.htm HTTP/1.1               <====== HERE, WE HAVE A POST REQUEST INSTEAD OF GET
Host: 192.168.1.12
Accept: */*
Content-Length: 50
Content-Type: application/x-www-form-urlencoded

* Operation timed out after 3233 milliseconds with 0 bytes received
* Closing connection

By the way, I'm using an Alpine 3.18 docker image with PHP 8.1.