hi all
I am new to php and curl.How can we create cookies using curl.What is the advantage of creating cookies this way,instead of creating from php's normal way (Setcookie());
Thanks to all
Hi,
CURL is used by your PHP server to make a Request to another Web Server.
Here your websever is acting as the HTTP Client (like your browser) initiating the HTTP Request to the remote server.
Setcookie() is a request to set a cookie sent from your web server to the HTTP Client (usually a browser) that made the initial HTTP Request.How Setcookie() works:
To understand this you need to understand how cookies are implemented.
Since HTTP is stateless, Cookies were described in the HTTP Specifications as a way to save data between 2 HTTP Requests.
An example HTTP GET Request looks like this:
GET / HTTP/1.0
Host: www.example.com
This is a request for the index page on the domain: www.example.com
Lets say you have the PHP Script below on the index.php page on www.example.com .
<html><head><title>Example</title></head>
<body>
<?php
setCookie('test', 'Test Cookie');
echo 'Test: '.$COOKIE['test'];
?>
</body>
</html>
The HTTP Response can be something like:
HTTP/1.0 200 OK
LOCATION: www.example.com
Set-Cookie: test=Test Cookie
Connection: close
<html><head><title>Example</title></head>
<body>
Test:
</body>
</html>
Note the HTTP Header:Set-Cookie: test=Test Cookie
This was generated by your webserver when it executed the function setCookie('test', 'Test Cookie');
Now when the web browser receives the HTTP Response from your server, it will save the cookie to its cookies file/folder.
Every time it sends a HTTP Request to the domain www.example.com in the future, it will also send it the cookie.
Example:
GET / HTTP/1.0
Host: www.example.com
Cookie: test=Test Cookie
Now The response from the index.php page on example.com will be:
HTTP/1.0 200 OK
LOCATION: www.example.com
Set-Cookie: test=Test Cookie
Connection: close
<html><head><title>Example</title></head>
<body>
Test: Test Cookie
</body>
</html>
Notice that $COOKIE['test'] now exists in the server as "Test Cookie". You can see this in the HTTP Response Body.How CURL Works:
Now in the above example, the HTTP Session was between a Browser and the Web Server at the domain www.example.com
Now say you have a PHP web server at www.myexample.com .
CURL allows your PHP server at www.myexample.com to create a HTTP Request to www.example.com as if it were a web browser. This is what server to server communication is and is the purpose of CURL. It allows your PHP server to act as an HTTP Client.
Say you have the php code below on www.myexample.com:
<?php
$ch = curl_init("http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$HTTP_Response = curl_exec($ch);
curl_close($ch);
?>
This will make CURL send a HTTP Request such as the one below to www.example.com:
GET / HTTP/1.0
Host: www.example.com
Notice this is just like the HTTP Request the browser made.
Now this is the same request for index.php so example.com will respond with the same HTTP Response as it did to the browser. To the server at www.example.com , the HTTP Request is from just another HTTP Client.
HTTP/1.0 200 OK
LOCATION: www.example.com
Set-Cookie: test=Test Cookie
Connection: close
<html><head><title>Example</title></head>
<body>
Test:
</body>
</html>
This HTTP Response will be saved to $HTTP_Response in our script as we specified in the line:
$HTTP_Response = curl_exec($ch);
If you notice, in the HTTP Response, the server at www.example.com requested that we set a cookie:
Set-Cookie: test=Test Cookie
Now if you want this cookie saved so that you can send it in the next request, you have to instruct CURL to save cookies to a file using:
curl_setopt($ch, CURLOPT_COOKIEJAR, $file_path);
where $file_path is a path to a file on your server. Usually you want this file under the web root so it can't be accessed from HTTP Clients. :)
Then when you make a subsequent HTTP Request to the same domain, you need to specify the file where cookies is saved to CURL.
curl_setopt($ch, CURLOPT_COOKIEFILE, $file_path);
where $file_path is the path to the file used to save cookies to in the last request, or containing any cookies you want to use in netscape cookies format or raw HTTP Cookie Header format.
So on www.myexample.com you can have the PHP Code:
<?php
// file to save cookies to
$file_path = '/tmp/cookies.txt';
$ch = curl_init("http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $file_path);
$HTTP_Response1 = curl_exec($ch);
curl_close($ch);
$ch = curl_init("http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $file_path);
$HTTP_Response2 = curl_exec($ch);
?>
Now the second HTTP REquest to www.example.com will send the cookie set in the first HTTP Request.
Example: (second HTTP Request)
GET / HTTP/1.0
Host: www.example.com
Cookie: test=Test Cookie
For practical implementations, you can use CURLs ability to save cookies to have your webserver log into remote sites and do tasks for you just as if you were doing it from the browser.
PHP also has inbuilt support for creating Socket Based or TCP connections that are the underlying protocol HTTP is built on, and thus make HTTP Requests.
See: fsockopen()