Link to home
Start Free TrialLog in
Avatar of jpegvarn
jpegvarnFlag for United States of America

asked on

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;
?>
Avatar of hernst42
hernst42
Flag of Germany image

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.
Avatar of jpegvarn

ASKER

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?



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 ?
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.
What kind of sotware is running on that port you connect to. It is a webserver or some other software.
It a customized program that spits out XML when you send a query to its database in XML, that's all I know.
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;

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?
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany 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
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>";
 }
?>