Solved

Multiple requests with a single socket using fsockopen and ssl.

Posted on 2008-06-23
3
1,414 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

785 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