Maybe create a PHP class? How to trim and make an example script more dynamic
Looks like the question got deleted when I was pasting code.
I have included two functions below. One is a function for logging in and obtaining a session id, and the latter function is for getting some meta data, which uses the session id obtained form the Login(); function.
I have a feeling that this script can be cleaned up immensely, but every time I attempt to do so it breaks.
Is there a more elegant way of sending data than fput? Is there a more elegant way of parsing an XML response that using the between(); before(); and after(); functions? Can this script be made more dynamic to eventually be used in a library of sorts?
I have an understanding of how PHP classes can be used, but I have no where to start.
<?php
function Login () {
// Host, Servlet, Port, and Time Out information
$host='host.example.com';
$servlet='XMLAPI';
$port='80';
$time_out='20';
// Username and Password Variables
$username = 'SomeUserId';
$password = 'Somepassword';
$sock = fsockopen ($host, $port, $errno, $errstr, $time_out);
$data = "xml=<?xml version=\"1.0\"?><Envelope><Body><Login>";
$data .= "<USERNAME>" . $username . "</USERNAME&g开发者_开发问答t;";
$data .= "<PASSWORD>" . $password . "</PASSWORD>";
$data .= "</Login></Body></Envelope>";
$size = strlen ($data);
if (!$sock) {
print("Could not connect to host:". $errno . $errstr);
return (false);
}
fputs ($sock, "POST /servlet/" . $servlet . " HTTP/1.1\n");
fputs ($sock, "Host: " . $host . "\n");
fputs ($sock, "Content-type: application/x-www-form-urlencoded\n");
fputs ($sock, "Content-length: " . $size . "\n");
fputs ($sock, "Connection: close\n\n");
fputs ($sock, $data);
$buffer = "";
while (!feof ($sock)) {
$buffer .= fgets ($sock);
}
fclose ($sock);
//print ($buffer);
return ($buffer);
}
$xml_response = Login();
session_start();
$_SESSION['JsessionID'] = between ("<SESSION_ENCODING>","</SESSION_ENCODING>", $xml_response);
function GetMetaData () {
// List metadata id
$list_id = "7238776";
// Assign JSessionID from Login();
$JsessionID = $_SESSION['JsessionID'];
// Host, Servlet, Port, and Time Out information
$host='host.example.com';
$servlet = 'XMLAPI' . $JsessionID;
$port='80';
$time_out='20';
$sock = fsockopen ($host, $port, $errno, $errstr, $time_out);
if (!$sock) {
print("Could not connect to host:". $errno . $errstr);
return (false);
}
$data = "xml=<?xml version=\"1.0\"?><Envelope><Body>";
$data .= "<GetListMetaData><LIST_ID>" . $list_id . "</LIST_ID>";
$data .= "</GetListMetaData></Body></Envelope>";
$size = strlen ($data);
fputs ($sock, "POST /servlet/" . $servlet . " HTTP/1.1\n");
fputs ($sock, "Host: " . $host . "\n");
fputs ($sock, "Content-type: application/x-www-form-urlencoded\n");
fputs ($sock, "Content-length: " . $size . "\n");
fputs ($sock, "Connection: close\n\n");
fputs ($sock, $data);
$buffer = "";
while (!feof ($sock)) {
$buffer .= fgets ($sock);
}
fclose ($sock);
print ($buffer);
return ($buffer);
}
//XML Parsing Functions
function between ($this, $that, $inthat) {
return before($that, after($this, $inthat));
};
function before ($this, $inthat) {
return substr($inthat, 0, strpos($inthat, $this));
};
function after ($this, $inthat) {
if (!is_bool(strpos($inthat, $this)))
return substr($inthat, strpos($inthat,$this)+strlen($this));
};
// XML Parsing of GetMetaData(); function
$xml_response = GetMetaData();
$id = between ("<ID>","</ID>", $xml_response);
$last_name = between ("<NAME>LAST_NAME</NAME>\n<VALUE>","</VALUE>", $xml_response);
print($id)
?>
Look into SoapClient. I suspect you are trying to make calls to a web service, and your code could be compressed to something like this:
$client = new SoapClient('http://path/to/wsdl');
$session_id = $client->__call('Login', array($username, $password));
$metadata = $client->__call('GetListMetaData', array($session_id, '7238776'));
$id = $metadata->id;
$last_name = $metadata->last_name;
精彩评论