0
I have this function that returns Cookies from a particular site, I’m learning HTTP HEADERS,
But I don’t know why this warning if I’m returning my cookies all okay, What’s wrong with my code?
Notice: Object of class stdClass could not be converted to int in C: wamp64 www tr api login.php on line 34
Here is my code:
<?php
function cookies() {
    $request = curl_init();
    curl_setopt_array($request, [
            CURLOPT_URL                         => 'https://tr.com',
            CURLOPT_CUSTOMREQUEST       => 'GET',
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_HEADER                  => true,
            CURLOPT_HEADERFUNCTION  => function($curl, $header) use (&$cookie) {
                if (stripos($header, 'Set-Cookie:') === 0) {
                    $cookieOBJ = new \stdClass();
                    $cookieOBJ->cookies = '';
                    if (preg_match_all('/^Set-Cookie: \s*([^;]*)/i', $header, $matches)) {
                        foreach ($matches[1] as $cookie) {
                            $cookie = explode('=', $cookie);
                            $key = (!empty($cookie[0])) ? $cookie[0] : '';
                            $val = (!empty($cookie[1])) ? $cookie[1] : '';
                            $cookieOBJ->{"$key"} = $val;
                            $cookieOBJ->cookies .= $key . '=' . $cookieOBJ->{"$key"} . '; ';
                        }
                    }
                    return $cookieOBJ;
                }
                return strlen($header);
            }
        ]
    );
    $response = curl_exec($request);
    curl_close($request);
}
var_dump(cookies());
Note: I need you to return me as follows:
C:\wamp64\www\tr\api\login.php:28:
object(stdClass)[2]
  public 'cookies' => string 'fm=; ' (length=5)
  public 'fm' => string '' (length=0)
And by using my variable $cookieOBJ return me all or $cookieOBJ->cookies, but if I use $cookieOBJ->fm gives me an error:
Notice: Undefined Property: stdClass::$fm in C: wamp64 www tr api login.php on line 28
What is the solution?
Ta all right, but I wanted, that returns up for example:
fm=0;and it’s returning me like this:public 'fm' => string '0' (length=1)– user93101
Ué, is returning just what you say you want in your post "Note: I need you to return me as follows:". If you want to access each value just do
$seuscookies = cookie();and then$seuscookies->fm. If you want to format all cookies, to keep everything together, usehttp_build_query($seuscookies, '', '; ', PHP_QUERY_RFC3986) . ';';, so everything will be the way "that now you want".– Inkeliz
It worked. Thank you.
– user93101