Solved

PHP send XML request for results through socket

Posted on 2004-08-16
10
4,532 Views
Last Modified: 2013-12-12
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;
?>
0
Comment
Question by:jpegvarn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 11813097
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
 

Author Comment

by:jpegvarn
ID: 11813145
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
 
LVL 48

Expert Comment

by:hernst42
ID: 11813467
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jpegvarn
ID: 11813502
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
 
LVL 48

Expert Comment

by:hernst42
ID: 11813621
What kind of sotware is running on that port you connect to. It is a webserver or some other software.
0
 

Author Comment

by:jpegvarn
ID: 11813886
It a customized program that spits out XML when you send a query to its database in XML, that's all I know.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 11814028
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
 

Author Comment

by:jpegvarn
ID: 11814240
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
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 11814788
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
 

Author Comment

by:jpegvarn
ID: 11832360
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

730 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