Solved

Multiple requests with a single socket using fsockopen and ssl.

Posted on 2008-06-23
3
1,419 Views
Last Modified: 2013-12-25
I am writing a time sensitive script to submit socket-based (XML) requests at a certain time, in competition with other people running similar scripts.  There are time limits on how often I can send requests and a limit that I can only have one connection open at a time.  This leads me to belive that it's possible to have more than one request per connection. Is this correct? if so, how do I implement it?

Or, is it the case that a socket always get closed when a response is sent?

My best guess at implementation is below, but feels far from right.

Thanks!

$fp = fsockopen(example.com, 443);
 
$out = "GET /page1.html HTTP/1.1\r\n";
$out .= "Host: https://example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
  while (!feof($fp)) {
  echo fgets($fp, 128);
}
 
 
$out = "GET /page2.html HTTP/1.1\r\n";
$out .= "Host: https://example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
  while (!feof($fp)) {
  echo fgets($fp, 128);
}
 
fclose($fp);

Open in new window

0
Comment
Question by:mohrc
3 Comments
 
LVL 20

Expert Comment

by:virmaior
ID: 21855792
I don't believe what you are doing will work..

it would depend on how their socket is coded.

At the very least, you will need to send an EOF (or equivalent device) to tell it when to end a statement.  

As written, you are not signaling the server that a new request is being made so the subsequent requests will not work.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 250 total points
ID: 21868300
> $fp = fsockopen(example.com, 443);
are you "speaking" plain text on a SSL port? Just wondering?

Back to you question:
> .. it's possible to have more than one request per connection.
yes, use:

$out .= "Connection: keep-alive\r\n\r\n";


but depends on the server if it allows it.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to dynamically set the form action using jQuery.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question