Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send cookie with file_get_contents

Tags:

php

The example on PHP manual shows how you can use stream contexts to send a cookie. Here is the excerpt:

// Create a stream $opts = array(   'http'=>array(     'method'=>"GET",     'header'=>"Accept-language: en\r\n" .               "Cookie: foo=bar\r\n"   ) );  $context = stream_context_create($opts);  // Open the file using the HTTP headers set above $file = file_get_contents('http://www.example.com/', false, $context); 

How do you send more than one cookie? Like #1 or #2, or what?

#1

"Cookie: user=3345&pass=abcd\r\n" 

#2

"Cookie: user=3345\r\n" .  "Cookie: pass=abcd\r\n" 
like image 703
Majid Fouladpour Avatar asked Aug 07 '10 16:08

Majid Fouladpour


People also ask

Is cURL faster than file_get_contents?

This is old topic but on my last test on one my API, cURL is faster and more stable. Sometimes file_get_contents on larger request need over 5 seconds when cURL need only from 1.4 to 1.9 seconds what is double faster.

What will the file_get_contents () return?

The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .

How does file_get_contents work?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.


2 Answers

#3

Cookie: user=3345; pass=abcd 
like image 194
mvds Avatar answered Sep 19 '22 04:09

mvds


Both aren't correct. You separate them with ;:

Cookie: user=3345; pass=abcd 
like image 28
NikiC Avatar answered Sep 20 '22 04:09

NikiC