Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket & Http...

Status
Not open for further replies.

iranor

Programmer
Jun 17, 2004
174
CA
Here, i'm trying to connect to a page to put the content in a string to parse it later. I can't use the server IP address because it's hidden.
Code:
$fp = fsockopen ('[URL unfurl="true"]http://quebecpeche.com',[/URL] 80, $errno, $errstr, 120);
$out = "GET /index.php HTTP/1.1\r\n";
$out .= "Host: [URL unfurl="true"]www.quebecpeche.com\r\n";[/URL]
$out .= "Connection: close\r\n\r\n";

fwrite ($fp, $out);
while (! feof ($fp))
{
   $content .= fgets ($fp, 4096);
}
fclose ($fp);

When I run that page, I get the following error:
Code:
Warning: fsockopen() [function.fsockopen]: unable to connect to [URL unfurl="true"]http://quebecpeche.com:80[/URL] (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in /var/[URL unfurl="true"]www/html/test.php[/URL] on line 1

What can I do to fix that?
 
(Sorry for double post, can't find how to edit)

I use PHP 5.0.3 with Apache 2. I have no problem parsing the site using the IP address, but it won't let me parse it using the http:// address.
 
I am not that familar with fsockopen(), but I believe your first line should be:
Code:
$fp = fsockopen ('quebecpeche.com', 80, $errno, $errstr, 120);
Take out the ' from the first argument.

HTH
Itshim
 
(Sorry for double post, looks like the forums could really use an edit button)

Anyway...

The IP Address for that website is: 66.98.244.17

The website is on a server which hosts multiple websites. The 'Host: tells the server which website you are looking for.

I tested both the IP Address and domain name on my machine and everything went well.

HTH,
Itshim
 
Ahh, thanks a lot!

The IP address wasn't working... It redirect to the hosting's website.

One more question...

Is there a way to make php save a cookie the site send to him and use it to open the site?
 
I've never done this, but yes you should be able to capture the cookie information sent back to PHP. The cookie is sent in the headers when PHP retrieves the information resquested from the host server. I believe the incoming cookie header should start with:

Set-Cookie: cookie information...

There may also be multiple cookies sent. So you will need to capture all the cookie(ies) info, from the incoming headers. Then when you go to connect to site again you will need to add that cookie information to the headers you send the server. The header to send cookie information I believe looks like this:

Cookie: cookie information...

I'm sorry I could not be more specific in my answer, but I like I said I have not done this before. Maybe someone else around here can be more specific.

Thanks,
Itshim
 
Well, take out the http: was a good advice (in fsockopen() case). But cookies exists in http scope only...
 
I'm not trying to start and argument, but after reading ArkM's post, I decided to do some testing on the cookie issue. I thought since you are sending the header:

GET /index.php HTTP/1.1\r\n

You were working in the http scope...

What I did was...
First I altered the code above to load the tek-tips home page. I ran the code and verified that I get back 'Set Cookie: ' headers.

Then since my login here at tek-tips, is set to 'remember me', which stores a cookie on my machine; I first loaded the tek-tips home page through the code posted above, and of course it loaded the page as if I was not logged in. I then added the a 'Cookie: ' header to the headers I sent and this time the page loaded and I was logged in.

Now I understand that this is not a complete test of the idea above but it should stand to reason that if I collect the contents of the page being contacted, which includes all the headers. Then break up the headers sent back to obtain the cookie information, I should be able to add that cookie information to the headers the next time I contact that page, eh?

Again I am not trying to start an argument, I just want to what I am missing, or misunderstanding.

Thanks,
Itshim
 
If you are using HTTP, try CURL that allows you to manipulate cookies (I'm told)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top