I want to remote-copy a file from a server to my server using PHP. the server that contains the file needs authorization, so i put the authorization (user/pass) in my request, like this:
1- open socket connection
$fp=fsockopen (....
2- set the request like this :
$request = POST . " " . $url . " HTTP/1.1" . $nn . "Host: " . www.example.com:80 . $nn . "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14" . $nn . "Accept: */*" . $nn . "Accept-Language: en-us;q=0.7,en;q=0.3" . $nn . "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7" . $nn . "Pragma: no-cache" . $nn . "Cache-Control: no-chache" . $nn . "Connection: Close"
3- send request like:
fputs ( $fp, $request );
this will work, but when a website uses SESSION, it sends cookies to the browser and every time the browser wants something from the website, the website server checks the cookies and responds.
So, when I am requesting to a remote server from a PHP code (i.e I have no browser to save cookies) What can I do to receive and store the cookies which the remote server sends?
I know how to use cookies in my request, I just include the cookie into my request like this :
$request = POST . " " . $url . " HTTP/1.1" . $nn . "Host: " . www.example.com:80 . $nn . "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Fir开发者_Go百科efox/2.0.0.14" . $nn . "Accept: */*" . $nn . "Accept-Language: en-us;q=0.7,en;q=0.3" . $nn . "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7" . $nn . "Pragma: no-cache" . $nn . "Cache-Control: no-chache" . $nn . $cookies . "Connection: Close"
I don't know how I can receive and store the cookies :(
the idea is
1- send a first request without cookies
2- receive and store cookies
3 send request with cookies and fly:)
how I can do NO.2 ?
another suggestion : can i read cookies from header : look at this one
HTTP/1.1 302 Moved Temporarily Content-length: 0 Content-type: text/html Server: Sun-ONE-Web-Server/6.1 Date: Mon, 28 Dec 2009 11:01:05 GMT Set-cookie: ERIGHTS=VQPFIWEdiYEtW4Eko2T3bikz4H2dDx2FtH-zlTTRFyzW+urkCU7bwGj2w==;path=/;domain=.example.org Set-cookie: WLSESSION=1512202892.20480.0000; expires=Tue, 29-Dec-2009 11:01:06 GMT path=/ Location: http://example.org/example
Have you considered using Zend_HTTP. It has very good cookie handling.
$client = new Zend_HTTP_Client();
$client->setCookieJar(); //now all cookies are stored for this Zend_HTTP_Client
精彩评论