Link to home
Start Free TrialLog in
Avatar of DSNews
DSNewsFlag for United States of America

asked on

PHP Connect to SSL Socket with a POST request

Hey experts, I got a nice one that I can't seem to find the answer to anywhere on the net.  

I'm trying to set up a script that talks to a secure server.  Once connected, the server will send a non stop data stream that I need to handle.  The only way I can see this working where I would still have control in the script is to access the data stream with something like fsockopen, but I have to initiate the stream with a POST...  I'm thinking this might be outside the limits of PHP, but I'd like to think not.  cURL would be the best approach, but I can't figure out how to get the result from cURL stored into a buffer, and not have to wait for the full returned result (which would never happen due to the never ending datastream).  Here is some pseudo code for clarification.  Seems simple enough to me, but I don't think PHP has pointers, even though it's built in functions sometimes return them (i.e.: fopen).

Please educate me.  I've gone through all of my PHP books (yeah I RTFM =D), and I've googled for a few days on this topic, but the solution eludes me.

Thanks in advance!
<?
// Initialize the connection and store result into pointer or buffer
$pointer = OpenConnectionSomehow('https://www.example.com', $post_data);
 
// Do stuff with data
while($pointer){
 
    DoSomething($pointer);
 
}
 
?>

Open in new window

Avatar of DSNews
DSNews
Flag of United States of America image

ASKER

I've been doing more research, and I came across using CURL contexts in fopen(), but I'm not quite sure what that does.  I am going to try and see if this does what I want it to do or not.  Lets hope for the best!
Avatar of markh789
markh789

cURL will work with https:
From http://curl.haxx.se/:
curl is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks.


< ?php
function OpenConnection($url) {
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL,$url);
 
// Change 1 to how many parameter's you are going to post
curl_setopt($ch, CURLOPT_POST, 1);
 
// HERE IS WHERE YOU CAN PUT POST DATA
curl_setopt($ch, CURLOPT_POSTFIELDS,"par1=value1;par2=value2");
 
$result= curl_exec ($ch);
curl_close ($ch);
return $result;
}
 
$the_result = OpenConnection("https://www.example.com");
 
echo "We have data...:<BR> " . $the_result;
?>

Open in new window

Avatar of DSNews

ASKER

That would work fine if I didn't need to parse the data as it came in.  Since the connection will not send an end of file, wouldn't that kind of dead lock the script?  I need to be able to read the input in chunks so that I can parse the data as it comes in.  Another factor that has to be addressed is that the data stream will be deflated (gzip).  Imagine it this way.  You have a server that when you connect will send you no stop data on a stock listed in NASDAQ, pretty much a string with a delimiter and an occasional heartbeat to keep the connection alive.  With the previous stated method, you wouldn't be able to parse the data as it came in, only when the connection was terminated, thus sending the return to the variable and passing control back to the main function(I learned PHP through C++).  If this is incorrect, let me know.  The only other way that I can see this working is if there were some way to initiate an input buffer (which is what fopen seems to do essentially).  I think what I need may be achieved with the concept in the following code from http://us3.php.net/manual/en/context.curl.php.

<?php
 
$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);
 
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
 
$context  = stream_context_create($opts);
 
// Change file_get_contents to fopen and use fread to get chunks for parsing
$result = file_get_contents('http://example.com/submit.php', false, $context);
 
?>

Open in new window

What do you want to do with the retried data? and what kind of data would you be receiving  ?
Avatar of DSNews

ASKER

Essentially, I want to take the data as it arrives and pass it on to necessary objects that will do something with the data.  Those objects will do some stats work on it, and determine if it needs further processing or not.  Due to NDA's and what not, I can't elaborate much more than that.  The data I would be receiving would be a string that would be delimited by semicolons.  On top of that, it will be gzipped but that can be solved later.
I don't understand what you want to happen with that data, if you want to pass it through $objects->like("this")['OOP']; then you can easily set-up a class to do that. What kind of stats work do you want done, tell me what information you want to retrieve.
ASKER CERTIFIED SOLUTION
Avatar of DSNews
DSNews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial