473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pear and php

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
7 2696
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sandro Dentella | last post by:
I need to use a class to handle mbox files that uses PEAR. Since I'm using Debian (both woody and, I simply apt-get(ed) php4-pear but I can't use it becouse I get the error: Warning: dl():...
3
by: junkmail | last post by:
on a win 2k server apparantly it is saying i dont have it. or it cant find it. im using mySQL 4.1 php 4.3.x apache 3.0.53 im new to php and am doing some tutorials, but when i run the sripts...
1
by: DJ Majestik | last post by:
OK, I am new to the whole PEAR/Smarty templating thing. I am trying to setup my directory structure correctly. If someone could weigh in and see if I have this setup "right", I would appreciate it....
3
by: David | last post by:
I installed the Pear program and set the include path in php.ini to point to the pear dir. I have a program which <? require_once("DB.php"); ?> This produces the error "Class 'PEAR_Error'...
2
by: Alan Prescott | last post by:
I'm running SuSE Linux 9.3, fully up to date running # pear install db responds with ... downloading DB-1.7.6.tgz ... Starting to download DB-1.7.6.tgz (124,807 bytes)...
5
by: Jim Michaels | last post by:
I downloaded a PEAR module, but it came with this XML file and a PHP file. I figure I just include the PHP file somehow, but what do I do with the XML file?
0
by: bwhitehd | last post by:
I'm having a problem installing php. The compile seems to go fine, but when it gets to the install step I get the following errors. Does anyone have an idea of what might be the problem? We...
1
by: IchBin | last post by:
I am new to PHP and I am running on Windoze XP SP 2. Just noticed that the PEAR installed dir structure I have (to DB subdir) is : C:\php5.2\PEAR\pear\DB My registry vars are:...
1
by: CSTechie | last post by:
I've been battling to install PEAR on Windows XP now for too long. I am not sure what I need to do. When I run go-pear.php from the command line, I get the error message as shown at the end. I...
1
by: IchBin | last post by:
I remember having problems with pear when I first installed it but got it running some time ago. This is on a windoze XP SP OS. I did not think it was correct because it pointed to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.