CURLOPT_HEADERFUNCTION - Return of cookies

0

I have this function that returns Cookies of a particular site, I am learning HTTP HEADERS ,

But I do not know why this warning if I'm returning my cookies ok , What's wrong with my code?

  

Notice: Object of class stdClass could not be converted to int   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 like this:

C:\wamp64\www\tr\api\login.php:28:
object(stdClass)[2]
  public 'cookies' => string 'fm=; ' (length=5)
  public 'fm' => string '' (length=0)

And when I use my variable $cookieOBJ it returns all or $cookieOBJ->cookies , but if I use $cookieOBJ->fm it returns an error:

  

Notice: Undefined property: stdClass :: $ fm in   C: \ wamp64 \ www \ tr \ api \ login.php on line 28

What is the solution?

    
asked by anonymous 07.10.2017 / 23:42

1 answer

1

This code will never work, first see the documentation for CURLOPT_HEADERFUNCTION :

  

The header callback will be called once for each header and only complete header lines are passed on to the callback.

If you go to you've even used , including http://"stackoverflow.com/a/215803/15089"> should have come from here and

08.10.2017 / 12:41