By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,405 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,405 IT Pros & Developers. It's quick & easy.

Pear and php

P: n/a
Does anyone know of any good sites with examples of php and pear - The
documation on the pear.php site is a bit above me as it doesn't show
any examples unlike the php.net site.

I'm mainly looking for examples of the http requests like posting and
retreiving from sites that require cookies..

Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Q: How do I retrieve a page from a web site that requires a cookie?
A: Use stream_context_create() to create a HTTP context with Cookie as
one of the headers. Then, if you are coding in PHP 5, pass the context
to file() or file_get_contents() as the third parameter. In PHP 4
either
function accepts a context, so you need to open the URL with fopen()
and
retrieve the data a chunk at a time with fread().

Example:

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>
"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);

$context = stream_context_create($opts);
$f = fopen($url, "rb", false, $context);
while($data = fread($f, 1024)) {
echo $data;

}

stream_context_create() is available in PHP 4.3.0 and above. If you are
using an older version, you would need the cURL functions or use
fsockopen() to open the connection and send the cookie header with
fputs().

Example 1:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Cookie: foo=bar"));
curl_exec($ch);
curl_close($ch);

Example 2:

$fp = fsockopen($host, $port);
fputs($fp, "GET / HTTP/1.0\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Cookie: foo=bar\r\n\r\n");

while ($data = fgets($fp, 1024)) {
echo $data;

}

Jul 17 '05 #2

P: n/a
Just get the page pretending that you're IE. Add "User-Agent:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)" to the HTTP
headers.

Jul 17 '05 #3

P: n/a
On 3 May 2005 14:56:55 -0700, "fs*******@gmail.com" <fs*******@gmail.com>
wrote:
Thanks Chung, I've managed to get a response using fsockopen but the
data appears to be encoded


If you use plain fsockopen you'll end up rewriting an HTTP client, which means
you'll have all sorts of potential complications to either deal with, or miss
out (and have it break later). Chung Leong pointed out two other ways of doing
this; with PHP streams, or with cURL, both of which implement HTTP clients so
you don't have to deal with anywhere near as much.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4

P: n/a
I was sending the same headers as a browser request and there's quite a
few.The info is gzip encoded I just assume that IE6 inflates it.

fputs($fp, "Accept: image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/vnd.ms-excel, application/msword,
application/x-shockwave-flash, */*\r\n");
fputs($fp, "Referer:
http://www.betfair.com/betting/LoadBetsDataAction.do?mi=2702263&SO=BL&MB=on&AR=on &MBV=AVG&BI=on\r\n");

fputs($fp, "Accept-Language: en-gb\r\n");
fputs($fp, "Accept-Encoding: gzip, deflate\r\n");
fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; www.ASPSimply.com; .NET CLR 1.0.3705; .NET CLR 1.1.4322)\r\n");
fputs($fp, "Host: $host\r\n");

the above plus all the cookie data

require_once "Request.php";
$req =& new HTTP_Request("http://www.betfair.com/");
$response = $req->sendRequest();
if (PEAR::isError($response)) {
echo $response->getMessage();
} else {
print_r($req->getResponseCookies());
}
echo $response;

I've tried curl but with no luck thats why I wanted to give pear a go
but couldn't find any decent examples to start off.

Jul 17 '05 #5

P: n/a
I was sending the same headers as a browser request and there's quite a
few.The info is gzip encoded I just assume that IE6 inflates it.

fputs($fp, "Accept: image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/vnd.ms-excel, application/msword,
application/x-shockwave-flash, */*\r\n");
fputs($fp, "Referer:
http://www.betfair.com/betting/LoadBetsDataAction.do?mi=2702263&SO=BL&MB=on&AR=on &MBV=AVG&BI=on\r\n");

fputs($fp, "Accept-Language: en-gb\r\n");
fputs($fp, "Accept-Encoding: gzip, deflate\r\n");
fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; www.ASPSimply.com; .NET CLR 1.0.3705; .NET CLR 1.1.4322)\r\n");
fputs($fp, "Host: $host\r\n");

the above plus all the cookie data

require_once "Request.php";
$req =& new HTTP_Request("http://www.betfair.com/");
$response = $req->sendRequest();
if (PEAR::isError($response)) {
echo $response->getMessage();
} else {
print_r($req->getResponseCookies());
}
echo $response;

I've tried curl but with no luck thats why I wanted to give pear a go
but couldn't find any decent examples to start off.

Jul 17 '05 #6

P: n/a
Managed to finally get the data unencoded by ommitting the line

fputs($fp, "Accept-Encoding: gzip, deflate\r\n");

and replacing it fputs($fp, "Accept: text/plain, text/html\r\n"); so
it assumes my browser doesn't accept gzip data.......now why didn't any
of you tell me that :)

Code does seem to take ages to connect though.

Have to see if there's a problem passing all the cookie data which is
long.

Thanks for the help anyone who contributed

Jul 17 '05 #7

P: n/a
OK all sorted, Chungs code didn't close the fsockopen connection soit
waited for the timeout before displaying the result

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.