For file_get_contents are there options similar to those of Curl?

Asked

Viewed 119 times

1

For file_get_contents there are options similar 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);
  • Is that a question or a statement? That’s why you’re picking up cookies with no content. And this request is missing. You run something that hasn’t been initialized

  • @Owl, it’s a question. I didn’t set the whole function because I imagined I’d be confused. The options I’m missing from the file_get_contentsare only those of the cookie. If you have something complete that does this with f_g_c, thank you.

1 answer

0

In fact you can even use the Sockets for this, it is logical that with the file_gets_contents will also get.

It is important to remember that in the case below I am not using the Netscape format, used by CURLOPT_COOKIEJAR. I don’t know if this is really necessary, in its place I’m using the format itself Cookie:, being Nome=Valor;.


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


Obtain the cookies:

All cookies are sent by the server in the header of the request, they have the name of Set-Cookie and its format is also standard. In this case we only want its name and value, then 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 obtains the header of the last request, so you can read all the headers, the array is divided by line. Therefore, each cycle of the foreach is a line. Each cookie is sent in 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'));
    }
}
//..

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

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

This will save all cookies in format:

Nome=Valor; NomeCookie=ValorDoCookie; Qualquer=Coisa

Using the Cookies:

It is already in the format we need, just insert this in your request, so use the 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 header of Cookie: containing the cookies previously saved.


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 will have to adapt all the above code for that purpose.

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

  • If it’s not too much to ask. You could set up a function with these options?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.