Solved

PHP Connect to SSL Socket with a POST request

Posted on 2009-05-14
7
899 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:DSNews
  • 4
  • 3
7 Comments
 

Author Comment

by:DSNews
ID: 24391928
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!
0
 
LVL 3

Expert Comment

by:markh789
ID: 24392830
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

0
 

Author Comment

by:DSNews
ID: 24392877
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

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:markh789
ID: 24394318
What do you want to do with the retried data? and what kind of data would you be receiving  ?
0
 

Author Comment

by:DSNews
ID: 24397082
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.
0
 
LVL 3

Expert Comment

by:markh789
ID: 24410672
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.
0
 

Accepted Solution

by:
DSNews earned 0 total points
ID: 24412973
I am not having an issue with handling the data.  My issue was with obtaining the data via a stream, but I figured it out over the weekend.  You CAN use fopen to perform posts on an https resource, but you have to use custom contexts where you define the post method and the data to be sent.  I had the right idea in the previous post, but I couldn't get it to work properly (probably because I wasn't sending a content length.  After really digging through google, I found this blog post http://dimuthuc.blogspot.com/2008/01/web-services-with-fopen.html that helped me out tremendously - specifically the following code.  The only thing really different between my code and the following code is that you use a string to store the post data, and not http_build_query(), and the header.
<?php
 

$data = "Here is my POST data"; // i.e.: $data = urlencode("post=data&do=something");
 

$opts = array(

'http'=>array(
 

'method'=>"POST",

'header'=>"User-Agent: My Own Http Client\r\n".

         "Content-length: " . strlen($data)."\r\n",

'content' => $data
 

)

);
 

$context = stream_context_create($opts);
 

$fp = fopen($url, 'r', false, $context);
 
 

/* you got the pointer to $fp, use any function like fgets, freed to read the buffer */

?>

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to count occurrences of each item in an array.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now