For file_get_contents are there similar options to those of cURL?

1

For file_get_contents are there similar options to those of cURL?

$cookie_file = "cookie1.txt";
curl_setopt($curl, CURLOPT_COOKIESESSION, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file); 
$str = curl_exec($curl);
    
asked by anonymous 10.10.2017 / 21:18

1 answer

0

You can actually use Sockets for this, of course with file_gets_contents you will also get it.

It's important to remember that in the case below I'm not using the Netscape format, used by CURLOPT_COOKIEJAR . I do not know if this is really necessary, instead I'm using the Cookie: itself format, being Nome=Valor; .

There may be native functions, but in any case you will be able to do this manually, which is what I will do.

Get Cookies:

All cookies are sent by the server in the request header, they are named Set-Cookie and their format is also default. In this case we want only his name and value, so you can do:

$resposta = file_get_contents('http://127.0.0.1/teste.php');
if($resposta === false){
    exit();
}

$cookie = [];

foreach($http_response_header as $cabecalho){

    // Se o cabeçalho for "Set-Cookie:" iremos fazer...  
    if(mb_stripos($cabecalho, 'Set-Cookie:', 0, '8bit') === 0){

        // Se houver ';' pegamos tudo antes do primeiro ';'
        $string = mb_stristr($cabecalho, ';', true, '8bit');

        // Se não houver ';' então todo o cabeçalho é o próprio "Nome=Valor"
        if($string === false){
            $string = $cabecalho;
        }

        // Removemos o 'Set-Cookie:' e adicionamos numa array:
        $cookie[] =  trim(mb_substr($string, 11, null, '8bit'));
    }
}

file_put_contents('cookies.txt', implode($cookie, '; '));

The $http_response_header gets the header of the last request, so it can read all the headers, the array is divided by line. Therefore, each cycle of foreach is one line. Each cookie is sent on different lines, with the same name Set-Cookie .

You can also use other solutions, including using REGEX for the same purpose above, another example would be:

//...
foreach($http_response_header as $cabecalho){
    if(mb_stripos($cabecalho, 'Set-Cookie:', 0, '8bit') === 0){
        $cookie[] =  trim(mb_substr(explode( ';', $cabecalho)[0], 11, null, '8bit'));
    }
}
//..

Now to save the information we use file_put_contents , in both cases:

file_put_contents('cookies.txt', implode($cookie, '; '));

This will save all cookies in the format:

Nome=Valor; NomeCookie=ValorDoCookie; Qualquer=Coisa

Using Cookies:

It is already in the format we need, just insert this in your request, so use stream_context_create :

$cookies = file_get_contents('cookies.txt');
$opcoes = [
    'http'=> [
        'header'=>
            'Cookie: '. $cookies . "\r\n"
    ]
];

$context = stream_context_create($opcoes);
$resposta = file_get_contents('http://127.0.0.1/teste.php', false, $context);

This will send the Cookie: header containing previously saved cookies.

If you need the Netscape format, which is used by CURLOPT_COOKIEJAR you are also able to store it in such a way. Of course, you'll have to adapt all the code above for that purpose.

Since CURLOPT_COOKIESESSION can not be "recreated" in the above case. This function removes session cookies, that is, discards cookies that do not have expiration time. In the case above we only get the name and value, we do not store the expiration time.

    
11.10.2017 / 14:19