/    Sign up×
Community /Pin to ProfileBookmark

Create and process a xml document

Hi !

I need do generate a XML document , something like this:
<USER>
<LOGIN>username</LOGIN>
<PASSWORD>userpass</PASSWORD>
</USER>
and send this to a document that is in [url]http://anywhere/scripts/somewebservice.dll/login[/url]

I’d like know how to create the xml document and how can i send this document to that URL. So, the program receive this xml and the login will happen.

Thanks in advance!

to post a comment
PHP

25 Comments(s)

Copy linkTweet thisAlerts:
@amrigoauthorJan 29.2006 — any idea?!
Copy linkTweet thisAlerts:
@chazzyJan 29.2006 — generating that document is merely echo'ing the various elements and their values. If you need help understanding how to create a regular XML document, i'd recommend the XML forum over this.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJan 29.2006 — can't you pass the values with sessions?

where would the values in the XML come from?

and, what version of PHP do you have?
Copy linkTweet thisAlerts:
@amrigoauthorJan 30.2006 — Hi ?

I already know about XML.

I just dont know how to pass this generated XMLto this url : ?

http://anywhere/scripts/somewebservice.dll/login

wich activates a webservice .

the xml have this format :

<?xml version="1.0" encoding=“ISO-8859-1” ?>

<USER>

<LOGIN>User</LOGIN>

<PASSWORD>Pass</PASSWORD>

<FORCELOGIN>value</FORCELOGIN>

</USER>

How can i do this ? :eek:
Copy linkTweet thisAlerts:
@Markbad311Jan 30.2006 — I will tell you right now your XML document will fail miserably if you need to have more then on entry in there. You need a Root Element

[code=html]
<?xml version="1.0" encoding=“ISO-8859-1” ?>
<users>
<user>
<login>User</login>
<password>Pass</password>
<forcelogin>value</forcelogin>
</user>
</users>
[/code]


Where is the Schema? Knowing about XML is good but to make XML documents that ARE extensible and VALID as well as WELL-FORMED is the key when creating ANY XML doc at anytime. I am an XML finatic. Use the Russian Doll Model it makes life 100% easier.
Copy linkTweet thisAlerts:
@NogDogJan 30.2006 — Perhaps use the [url=http://www.php.net/curl]Curl functions[/url]?
Copy linkTweet thisAlerts:
@amrigoauthorJan 30.2006 — How can i use curl to do this task ???

function send_xml( ){

$https_url ="https://somewebsite/scripts/somewebservice.dll/login";

$ch = curl_init( $https_url);

curl_setopt ($ch, CURLOPT_HEADER, 0);

curl_exec( $ch );

curl_close ($ch);

}


$ch = curl_init("http://somewebsite/scripts/somewebservice.dll/login/");

$fp = fopen("generatedxml.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);

curl_close($ch);

fclose($fp);
Copy linkTweet thisAlerts:
@amrigoauthorJan 30.2006 — How can i use curl to do this task ???

function send_xml( ){

$https_url ="https://somewebsite/scripts/somewebservice.dll/login";

$ch = curl_init( $https_url);

curl_setopt ($ch, CURLOPT_HEADER, 0);

curl_exec( $ch );

curl_close ($ch);

}


$ch = curl_init("http://somewebsite/scripts/somewebservice.dll/login/");

$fp = fopen("generatedxml.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);

curl_close($ch);

fclose($fp);
Copy linkTweet thisAlerts:
@amrigoauthorJan 31.2006 — Hi

May i can change the question :

How can i send a xml document to a know webservice like this:

http://somewebsite/scripts/somewebservice/login

the xml have this format :

<user>

<login>123</login>

<password>abc</password>

</user>

Thank´s in advance
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJan 31.2006 — where will the username and password be defined?
Copy linkTweet thisAlerts:
@amrigoauthorFeb 01.2006 — i will pass via post

this script i am trying returns me a error :

In a xml document only top level elements are allowed

: error at processing this url :

here is the script :

how it works and how can i fix this bug

<?

function Communicate($xml,$service) {

$HOST = "www.someweirdwebservice.com";

$sock = fsockopen($HOST, 80, $errno, $errstr, 30);

if (!$sock) {

die("$errstr ($errno)n");

}

fwrite($sock, "POST $service HTTP/1.0rn");
//fwrite($sock, "Host: secure.example.comrn");
fwrite($sock, "Content-type: application/x-www-form-urlencodedrn");
fwrite($sock, "Content-length: " . strlen($data) . "rn");
fwrite($sock, "Accept: */*rn");
fwrite($sock, "rn");
fwrite($sock, "$xmlrn");
fwrite($sock, "rn");

$headers = "";
while ($str = trim(fgets($sock, 4096)))
$headers .= "$strn";

echo "n";
$body = "";
while (!feof($sock))
$body .= fgets($sock, 4096);

fclose($sock);

return $body;
}

$USER = "loginhere";
$PASSWD = "passwdhere";
$service = "/scripts/SomeWebService.dll/login";
$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?><USER><LOGIN>loginhere</LOGIN><PASSWORD>passwdhere</PASSWORD></USER>';

echo Communicate($xml,$service);

?>

thank´s in advance!!
Copy linkTweet thisAlerts:
@chazzyFeb 01.2006 — ok...i think the issue's more like "Why are you doing this?"

  • - if you're using a web service, why does your communicate both write and read the same data? you should be sending the data over, therefore, you should only be writing it.

  • - the receiving script then opens the file and reads it.
  • Copy linkTweet thisAlerts:
    @amrigoauthorFeb 01.2006 — how can i fix it ?
    Copy linkTweet thisAlerts:
    @amrigoauthorFeb 03.2006 — Wich is the best way to do this task ?

    xml-rpc ? soap?

    I need do generate a XML document , something like this:

    <USER>

    <LOGIN>username</LOGIN>

    <PASSWORD>userpass</PASSWORD>

    </USER>

    and send this to a document that is in http://anywhere/scripts/somewebservice.dll/login

    I'd like know how to create the xml document and how can i send this document to that URL. So, the program receive this xml and the login will happen.
    Copy linkTweet thisAlerts:
    @chazzyFeb 03.2006 — Don't worry about processing the XML, unless you start handling the inbound response. You have to see how the webservice handles the input (ie does it recognize login and password parameters).

    Yes, use SOAP if that's what they're telling you to use. They should be providing you with the specifications that you need to follow as well.
    Copy linkTweet thisAlerts:
    @bokehFeb 03.2006 — In order to do this you need to know in what form the receiving URL is expecting the data and by what method. Without the answer to this question no one will be able to give you a valid answer.
    Copy linkTweet thisAlerts:
    @amrigoauthorFeb 03.2006 — i have to send xml via post method
    Copy linkTweet thisAlerts:
    @amrigoauthorFeb 03.2006 — Don't worry about processing the XML, unless you start handling the inbound response. You have to see how the webservice handles the input (ie does it recognize login and password parameters).

    Yes, use SOAP if that's what they're telling you to use. They should be providing you with the specifications that you need to follow as well.[/QUOTE]


    Hi!

    I have just the adress and the xml format and no more documentation at all!

    i know just the adress and the xml format
    Copy linkTweet thisAlerts:
    @chazzyFeb 03.2006 — But what are your specifications? What fields need to have values in the XML for it to work?
    Copy linkTweet thisAlerts:
    @bokehFeb 03.2006 — i have to send xml via post method[/QUOTE]Ok... you need to send by the post method but as what? A string? A file? what? Where can we read the specs.
    Copy linkTweet thisAlerts:
    @amrigoauthorFeb 06.2006 — The only specification i receive was :

    this url :

    http://anywhere/scripts/somewebservice.dll/login

    must receive this xml :

    <USER>

    <LOGIN>username</LOGIN>

    <PASSWORD>userpass</PASSWORD>

    </USER>

    and will send a xml back.
    Copy linkTweet thisAlerts:
    @bokehFeb 06.2006 — http://anywhere/scripts/somewebservice.dll/login[/QUOTE]With such a cryptic reply how can anyone help you?
    Copy linkTweet thisAlerts:
    @amrigoauthorFeb 06.2006 — Hi

    Yes i know !! but all i have is this spec:

    4)LOGIN :

    4.1) –Server Access:

    4.1.1) Caminho: /login

    Exemplo: http://localhost/scripts/SomeWebService.dll/login

    4.1.2) Request: (XML send from the software wich shows the data for the WebService)

    Send data of the user and password to access the server followning the example:

    <?xml version="1.0" encoding=“ISO-8859-1” ?>

    <USER>

    <LOGIN>Username</LOGIN>

    <PASSWORD>Password</PASSWORD>

    <FORCELOGIN>Value</FORCELOGIN> // 0= do not force login 1= Force login

    </USER>

    4.1.3) Answer from the WebService: (XML send from the WebService to the software wich shows the data).

    <USER>

    <ID>User identifier (ID)</ID>

    <CLIENT_ID>Identifier (ID) of the Client of the user</CLIENT_ID>

    <TYPE_ID>Type Identifier of the user</TYPE_ID>

    </USER>
    Copy linkTweet thisAlerts:
    @bokehFeb 06.2006 — Well you will need to get some proper instructions or give up.
    Copy linkTweet thisAlerts:
    @amrigoauthorFeb 06.2006 — i am trying to use Pear, Net with httprequest class
    [code=php]
    <?php
    // +-----------------------------------------------------------------------+
    // | Copyright (c) 2002-2003, Richard Heyes |
    // | All rights reserved. |
    // | |
    +-----------------------------------------------------------------------+
    // | Author: Richard Heyes <[email protected]> |
    // +-----------------------------------------------------------------------+
    //
    // $Id: Request.php,v 1.33 2003/10/26 10:28:28 avb Exp $
    //
    // HTTP_Request Class
    //
    // Simple example, (Fetches yahoo.com and displays it):
    //
    // $a = &new HTTP_Request('http://www.yahoo.com/');
    // $a->sendRequest();
    // echo $a->getResponseBody();
    //

    require_once('./Net/Socket.php');
    require_once('./Net/URL.php');

    define('HTTP_REQUEST_METHOD_GET', 'GET', true);
    define('HTTP_REQUEST_METHOD_HEAD', 'HEAD', true);
    define('HTTP_REQUEST_METHOD_POST', 'POST', true);
    define('HTTP_REQUEST_METHOD_PUT', 'PUT', true);
    define('HTTP_REQUEST_METHOD_DELETE', 'DELETE', true);
    define('HTTP_REQUEST_METHOD_OPTIONS', 'OPTIONS', true);
    define('HTTP_REQUEST_METHOD_TRACE', 'TRACE', true);

    define('HTTP_REQUEST_HTTP_VER_1_0', '1.0', true);
    define('HTTP_REQUEST_HTTP_VER_1_1', '1.1', true);

    class HTTP_Request {

    /**
    * Full url
    * @var string
    */
    var $_url;

    */
    function HTTP_Request($url, $params = array())
    {
    $this->_sock = &new Net_Socket();
    $this->_method = HTTP_REQUEST_METHOD_GET;
    $this->_http = HTTP_REQUEST_HTTP_VER_1_1;
    $this->_requestHeaders = array();
    $this->_postData = null;

    $this->_user = null;
    $this->_pass = null;

    $this->_proxy_host = null;
    $this->_proxy_port = null;
    $this->_proxy_user = null;
    $this->_proxy_pass = null;

    $this->_allowRedirects = false;
    $this->_maxRedirects = 3;
    $this->_redirects = 0;

    $this->_timeout = null;
    $this->_response = null;

    foreach ($params as $key => $value) {
    $this->{'_' . $key} = $value;
    }

    $this->setURL($url);

    // Default useragent
    $this->addHeader('User-Agent', 'PEAR HTTP_Request class ( http://pear.php.net/ )');

    // Default Content-Type
    $this->addHeader('Content-Type', 'application/x-www-form-urlencoded');

    // Make sure keepalives dont knobble us
    $this->addHeader('Connection', 'close');

    // Basic authentication
    if (!empty($this->_user)) {
    $this->_requestHeaders['Authorization'] = 'Basic ' . base64_encode($this->_user . ':' . $this->_pass);
    }

    // Use gzip encoding if possible
    if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && extension_loaded('zlib')) {
    $this->addHeader('Accept-Encoding', 'gzip');
    }
    }

    .... //all the function here

    /**
    * Notifies all registered listeners of an event.
    *
    * @param string Event name
    * @param mixed Additional data
    * @access private
    * @see HTTP_Request::_notify()
    */
    function _notify($event, $data = null)
    {
    foreach (array_keys($this->_listeners) as $id) {
    $this->_listeners[$id]->update($this, $event, $data);
    }
    }
    } // End class HTTP_Response
    ?>

    [/code]


    and calling the webservices using this code :
    [code=php]
    $XML = $XML_HEADER;
    $XML .= "<USER>";
    $XML .= "<LOGIN>$login</LOGIN>";
    $XML .= "<PASSWORD>$pass</PASSWORD>";
    $XML .= "</USER>";

    // logando no WebService
    require_once('Request.php');
    $Response = '';
    $Req = &new HTTP_Request($WebService . 'login');
    $Req->setMethod(HTTP_REQUEST_METHOD_POST);
    $Req->clearPostData();
    $Req->addRawPostData($XML,True);
    $Req->sendRequest();
    $Response = $Req->getResponseBody();
    [/code]


    I could not use this code from an external web server other than the one where the webservice is installed . webservices can be connfigured to beahave this way ? it can be configured to be acessed from a external webserver ? in order to make tests ?

    Thank´s in advance
    ×

    Success!

    Help @amrigo spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...