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
Solved

PHP send XML request for results through socket

Posted on 2004-08-16
10
4,510 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

808 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