PHP send XML request for results through socket

Hello,

I'm trying to send an XML request to get returned results through a socket connection.  I found the below code from some of the other posts and am still trying to customize it to my needs.  Am I on the right track?

myrequestfile.xml has the requested information and I want to return the results in a browser.

fputs($socket, "POST /test/expert.php HTTP/1.1\n"); <-- I'm confused on what expert.php is or what is supposed to contain.

Thanks for the replies.

<?php
$host = 'myhost';
$port = myport;
$timeout = 10;
$xml = file_get_contents('myrequestfile.xml');
$length = strlen($xml);
$socket = fsockopen($host, $port, $errno, $errstr, $timeout);
fputs($socket, "POST /test/expert.php HTTP/1.1\n");
fputs($socket, "User-Agent: shmertmethod\n");
fputs($socket, "Content-Type: text/xml\n");
fputs($socket, "Content-Length: $length\n");
fputs($socket, "\n");
fputs($socket, $xml);
$out = '';
while (!feof($socket)) {
    $out .= fgets($socket, 1024);
}
fclose($socket);
echo $out;
?>
jpegvarnAsked:
Who is Participating?
 
hernst42Connect With a Mentor Commented:
The problem is that the Server does not know when the XML-file is fully transmitted to the server. So to get this working you need the specification of that protocol (what must transmitted from the server and in which manner). Without that specification or a networksniffer I think it is impossible to get this working the correct way.
0
 
hernst42Commented:
The script looks good to make an xml-request to a certain page and get that output from it.

The expert.php script is located on the server where you want the content of your xml-file to be evaluated and you have connected to with socket_open. So expert.php is either written by you or is a script providey by someone other. If you have additional questions you may provide an example what you plan to do.
0
 
jpegvarnAuthor Commented:
The only thing I need to do here is open the socket, which is listed above, and send my SQL statments in XML format...which are in the myrequestfile.xml.  The return results will be the results from the SQL query in XML format from the HOST.

The XML-request is going to the Host through the socket, not to a page.  Does that make sense?



0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
hernst42Commented:
The script that should handle the XML-File (execute the queries stored in the xml-file) is it written by you or are you using a service on another server?
What is the URL of the script that handles the XML-file ?
0
 
jpegvarnAuthor Commented:
Correct,

The port I am connecting to answers in XML.  So, when I submit myrequestfile.xml (there is a <STATEMENT> tag in there for SQL statements)....I should get a return with my query results in XML format.
0
 
hernst42Commented:
What kind of sotware is running on that port you connect to. It is a webserver or some other software.
0
 
jpegvarnAuthor Commented:
It a customized program that spits out XML when you send a query to its database in XML, that's all I know.
0
 
hernst42Commented:
Then you need the specification of the protocol that is needed to deliver the XML-file to the server. Your code looks like as the xml-file should be posted to an webserver. Maybe it is suficient just to deliver the XML.

Try something like the following, but it may not work without knowing how the program excatly expects the input of the xml-file. If you have a program that uses that interface use a networksniffer and look how the xml-file of the other programm is delivered to the query-server.

$host = 'myhost';
$port = myport;
$timeout = 10;
$xml = file_get_contents('myrequestfile.xml');
$length = strlen($xml);
$socket = fsockopen($host, $port, $errno, $errstr, $timeout);
fputs($socket, $xml);
fputs($socket, "\n\n");
$out = '';
while (!feof($socket)) {
    $out .= fgets($socket, 1024);
}
fclose($socket);
echo $out;

0
 
jpegvarnAuthor Commented:
I know what format the xml file should be in.  It is already setup.

This is the furthest I've gotten, no error, but it times out...I set the php.ini file to timeout after 180 seconds which should be plenty...my SQL statement is querying very little data so it should return immediately.

Any other ideas?
0
 
jpegvarnAuthor Commented:
I've solved the problem.  FYI, here is what I used:

<?php

if(isset($_POST['submit']) && !empty($_POST['submit']))
{

$part_query = "";

if(isset($_POST['NUMBER']) && $_POST['NUMBER'] == 'NUMBER')
{
     $part_query .= empty($part_query) ? $_POST['NUMBER'] : " , ".$_POST['NUMBER'];
}

if(empty($part_query))  
  {
     echo "Please select any of the checkboxes to search";
     exit();
  }
   
}
                   
$where_query = "";
  if(isset($_POST['DateStart']))
{
     $where_query .= empty($where_query) ? $_POST['DateStart'] : " , ".$_POST['DateStart'];
}

if(isset($_POST['DateEnd']))
{
     $where_query .= empty($where_query) ? $_POST['DateEnd'] : " , ".$_POST['DateEnd'];
}

if(empty($where_query))  
 {
    echo "Please select a date range";
   exit();
}

//***************************************************
//***** Query Database

$DateStart = $_POST['DateStart'];
$DateEnd = $_POST['DateEnd'];
$query = "SELECT ".$part_query." FROM TABLENAME;
$query .=  " WHERE (DATE >= '$DateStart' AND DATE <= '$DateEnd')";
if (isset($query) && $query != "") {
// create a socket to the server
$socket = fsockopen("host", "port", $errno, $errstr, 5);
if (!$socket) {
echo $errno;
echo "<br>";
echo $errstr;
exit;
}
// put xml wrapper arround query
$myXML = "<?xml version='1.0'?>";
$myXML .= "<MYXML>";
$myXML .= "<USER>username</USER>";
$myXML .= "<PASSWD>password</PASSWD>";
$myXML .= "<DATABASE>database</DATABASE>";
$myXML .= "<STATEMENT>";
$myXML .= "<![CDATA[";
$myXML .= $query;
$myXML .= "]]>";
$myXML .= "</STATEMENT>";
$myXML .= "</MYXML>";

// write the xml stream to the socket
fwrite($socket, $myXML);

// get the response from the server and display it as is
do {
$myXML = fgets($socket, 1024);
echo $myXML;
 }
while(substr($myXML, 0, 12) != "</ResultSet>");

// close the socket
fclose($socket);
 }
else {
echo "<html><body>";
echo "<h3>Empty Request</h3>";
echo "</body></html>";
 }
?>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.