Maybe create a PHP class? How to trim and make an example script more dynamic
<p>Looks like the question got deleted when I was pasting code.</p>

<p>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. </p>

<p>I have a feeling that this script can be cleaned up immensely, but every time I attempt to do so it breaks.</p>

<p>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?</p>

<p>I have an understanding of how PHP classes can be used, but I have no where to start.  </p>

<pre><code>&lt;?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=&lt;?xml version=\"1.0\"?&gt;&lt;Envelope&gt;&lt;Body&gt;&lt;Login&gt;"; 
    $data .= "&lt;USERNAME&gt;" . $username . "&lt;/USERNAME&gt;"; 
    $data .= "&lt;PASSWORD&gt;" . $password . "&lt;/PASSWORD&gt;"; 
    $data .= "&lt;/Login&gt;&lt;/Body&gt;&lt;/Envelope&gt;"; 
    $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 ("&lt;SESSION_ENCODING&gt;","&lt;/SESSION_ENCODING&gt;", $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=&lt;?xml version=\"1.0\"?&gt;&lt;Envelope&gt;&lt;Body&gt;"; 
    $data .= "&lt;GetListMetaData&gt;&lt;LIST_ID&gt;" . $list_id . "&lt;/LIST_ID&gt;"; 
    $data .= "&lt;/GetListMetaData&gt;&lt;/Body&gt;&lt;/Envelope&gt;"; 
    $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 ("&lt;ID&gt;","&lt;/ID&gt;", $xml_response);
    $last_name = between ("&lt;NAME&gt;LAST_NAME&lt;/NAME&gt;\n&lt;VALUE&gt;","&lt;/VALUE&gt;", $xml_response);

    print($id)
    ?&gt; 
</code></pre>
