Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP send XML request for results through socket

Posted on 2004-08-16
10
Medium Priority
?
4,681 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

636 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