Forum Moderators: coopster

Message Too Old, No Replies

php xml parse question

         

omoutop

7:49 am on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hello all...

I need a guidance/tip/tutorial info on the following problem.

I submit some data to a site (https://example.com/one/two) from a form. The form has only two hidden fields. One field with a command to trigger some responce from the target url, and the second field holds some xml data.

The targeted site, gets the xml data and depending on the command send, process them. And then generates an xml report.

The new xml data are printed on screen (they do not generate some xml file).

My question is: how can my script read the xml report from the targeted site? My xml knowledge is limited and all tutorial I found are mostly for RSS feeds (in which case there is an actual xml file present to read).

Any help is appreciated. And thanks in advance.

phparion

9:10 am on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



there could be different ways for this One that I use frequently is with PHP-cURL: you can store the HTML Output of the cURL session in a variable. If required you can use regex to get pure xml output.

For this you will need to post your form in a cURL session i.e write cURL call in your form's action page and from there receive the HTML output in a variable.

omoutop

10:13 am on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



well i try cURL method of yours. I found a tutorial on how to grab an xml responce from a remote site, but i have some problems I can't figure out alone.

The script i use is:


//$data_xml_preauth holds some xml data

$send_form = "
<FORM name=\"myform\" id=\"myform\" method=\"POST\" action=\"https://target-example.com\">
<input type=\"hidden\" name=\"command\" value=\"NewRequest\">
<input type=\"hidden\" name=\"Data\" value=\"".htmlspecialchars($data_xml_preauth)."\">
</FORM>
";

$header[] = "Host: https://mysite.com";
$header[] = "Content-type: text/html";
$header[] = "Content-length: ".strlen($send_form) . "\r\n";
$header[] = $send_form;

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "https://target-example.com"); # URL to post to
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
curl_setopt( $ch, CURLOPT_HTTPHEADER, $send_form ); # custom headers, see above
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); # This POST is special, and uses its specified Content-type
$result = curl_exec( $ch ); # run!
curl_close($ch);

// the followinfg is for showing me the grab results
print "<textarea rows='10' cols='80'>";
print htmlentities($result);
print "</textarea>";

Now my problem is that the targeted site do not recognize the xml data i send. If I send only the xml data the target site do nothing - it requires the command also.
Also I think I must change something in the $header[] array, but I am confused (of course i change it to $header[] = "Content-type: text/xml";when i send only xml data (and not the whole form).

Any suggegions?

[edited by: eelixduppy at 10:34 am (utc) on April 20, 2007]
[edit reason] target-example.com [/edit]

phparion

10:28 am on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd prefer to post all required variables with cURL to the site link rather than putting all the form in the headers.

$POSTFIELDS = ''; //make a query string of your all form fields that you want to send e.g XML data and Command

$LOGINURL = "http://target-example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_REFERER, 'http://mysite.com');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/');
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/');
$result = curl_exec ($ch);
curl_close ($ch);

you can change the cookie path according to your requirements

if you still fail to get the xml output then it means site checks few things with your data for which I will recommend you to do HTTP Sniffing of the target site that how that proceed to generate xml output, if you are new to http sniffing then install an extension LIVE HTTP HEADER for firefox , open its window, generate the xml output on the target site in the normal mode you use and it will show you the process how the variables are working together on the target site.

[edited by: eelixduppy at 10:35 am (utc) on April 20, 2007]
[edit reason] target-example.com [/edit]

omoutop

10:34 am on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the insight. Putting the POST data in a string worked like a charm.

Now a final question: Is there a way to get the xml responce only instead of all the headers of the targeted site?

What is get is something like:

HTTP/1.1 100 Continue
Server:
Date:
X-Powered-By:
HTTP/1.1 200 OK
Server:
Date:
X-Powered-By:
Connection: close
Content-Type: text/xml; charset=iso-8859-1
Servlet-Engine:

<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE>
<ERRORCODE>0</ERRORCODE>
<ERRORMESSAGE></ERRORMESSAGE>
<REFERENCE>38e899b39838f6c19232f4d01df3addf</REFERENCE>
<PROXYPAYREF>2755167</PROXYPAYREF><SEQUENCE>0</SEQUENCE>
</RESPONSE>

Is there a way to ignore the headers and leave only the xml part?

[edited by: eelixduppy at 11:58 am (utc) on April 20, 2007]
[edit reason] fixed side scroll [/edit]

omoutop

11:43 am on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I figure it out....

i save the data into an xml file and then I retrieve what i need from the file...

Thanks again for all the help!

phparion

11:58 am on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you also had the option to use regex to fetch xml only.

omoutop

12:45 pm on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



regex? No way... Not familiar with the regular expressions at all... try to avoid them at every opportunity.

Thanks again for the help.