Twitter API with CURL

2

OBS: THE COMPLETE CODE IS HERE , but I made this adjustment to receive cookies

I'm trying to read the cookie files to make the requisite requests to get the result I expect, see my code:

$tokens = ['_twitter_sess', 'ct0', 'auth_token'];

        foreach (glob($this->_cookieFile) as $file) {
            $pointer = fopen($file, 'r');
        while (!feof($pointer)) {
        $line = fgets($pointer);

        foreach ($tokens as $token) {
          if (stripos($line, $token) !== false) {
            var_dump($line);
            curl_setopt_array($request, [
                                CURLOPT_COOKIEFILE  => $line,
                                CURLOPT_HTTPHEADER          => [
                          'origin: https://twitter.com',
                          'authorization: Bearer ' . $bearer,
                          'x-csrf-token: ' . $line,
                          'referer: https://twitter.com/',
                          'x-twitter-auth-type: OAuth2Session',
                          'x-twitter-active-user: yes',
                        ],
                         CURLOPT_POSTFIELDS => http_build_query([
                              'challenges_passed'   => false,
                              'handles_challenges'  => 1,
                              'include_blocked_by'  => true,
                              'include_blocking'    => true,
                              'include_can_dm'      => true,
                              'include_followed_by' => true,
                              'include_mute_edge'   => true,
                              'skip_status'         => true,
                              'user_id'             => Session::get('username'),
                                ], '', '&', PHP_QUERY_RFC3986),
                            ]
                        );
            break;
          }
        }
        }
        }

But giving var_dump to $line , I get a return:

Ineedtoreadeachlineinaarrayassociativoorreadallcookiesatonce.

SohowdoIreturnmycURLreturnsme:

'{"errors":[{"code":220,"message":"Your credentials do not allow access to this resource."}]}' (length=92)
    
asked by anonymous 06.09.2017 / 12:24

1 answer

3

If you really want an array, which for me does not make the slightest sense and it seems to me that this is a "XY problem". But anyway, just "explode" for every \t .

So, assuming it's in string:

$cookie = '# Netscape HTTP Cookie File

.example.org    TRUE    /   FALSE   1507286655  remember_me true
.example.org    TRUE    /   FALSE   1507286655  APISID  DijdSAOAjgwijnhFMndsjiejfdSDNSgfsikasASIfgijsowITITeoknsd
.example.org    TRUE    /   FALSE   1507286655  static_files    iy1aBf1JhQR';


$array = [];

foreach(array_slice(explode("\n", $cookie), 2) as $linha){

    $colunas = explode("\t", $linha);
    $array[$colunas[5]] = $colunas[6];

}

Result ( var_dump($array) ):

array(3) { ["remember_me"]=> string(5) "true " ["APISID"]=> string(58) "DijdSAOAjgwijnhFMndsjiejfdSDNSgfsikasASIfgijsowITITeoknsd " ["static_files"]=> string(11) "iy1aBf1JhQR" }

The idea is simple, ignore the first two lines ( array_slice(..., 2) ) then for each line break per tab ( \n ) and then get the name and value information.

Because I find this totally wrong:

  • You have native features for this in cURL, all features that use the cURL cookie management engine handle this Netscape format well.
  • You can use CURLOPT_HEADERFUNCTION to filter cookies if you are making a request to save such cookies.
  • You can also use CURLINFO_COOKIELIST to get the same information, filter it and save it, leaving only the data that matters.
  • You are storing unneeded data, making performance worse by having to filter it at all times, without any benefit.
  • 06.09.2017 / 13:02