Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1047943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:22:46+00:00 2026-05-16T16:22:46+00:00

Looks like the question got deleted when I was pasting code. I have included

  • 0

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>"; 
    $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)
    ?> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:22:47+00:00Added an answer on May 16, 2026 at 4:22 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This sounds like a look-up-in-the-manual question to me, but I can't find it. Suppose
Looks like here in StackOveflow there is a group of F# enthusiasts. I'd like
Looks like my only option is to create a mutatant child of the tree
It looks like we'll be adding CAPTCHA support to Stack Overflow. This is necessary
Update : Looks like the query does not throw any timeout. The connection is
It looks like I had a fundamental misunderstanding about C++ :< I like the
:vimgrep looks like a really useful thing. Here's how to use it: :vim[grep][!] /{pattern}/[g][j]
It looks like the standard asp.net 2 RangeValidator control doesn't allow times. e.g. 01/01/2008
It looks like the run-time compiler doesn't support the same language as the command-line
Update: looks like this is indeed a WiX limitation - Cannot add 64bit com+

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.