PHP Doku:: Generates XML for a PHP value - function.xmlrpc-encode.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzWeb ServicesXML-RPCXML-RPC Funktionenxmlrpc_encode

Ein Service von Reinhard Neidl - Webprogrammierung.

XML-RPC Funktionen

<<xmlrpc_encode_request

xmlrpc_get_type>>

xmlrpc_encode

(PHP 4 >= 4.1.0, PHP 5)

xmlrpc_encodeGenerates XML for a PHP value

Beschreibung

string xmlrpc_encode ( mixed $value )
Warnung

Diese Funktion ist EXPERIMENTELL. Das Verhalten, der Funktionsname und alles Andere, was hier dokumentiert ist, kann sich in zukünftigen PHP-Versionen ohne Ankündigung ändern. Seien Sie gewarnt und verwenden Sie diese Funktion auf eigenes Risiko.

Warnung

Diese Funktion ist bis jetzt nicht dokumentiert. Es steht nur die Liste der Argumente zur Verfügung.


5 BenutzerBeiträge:
- Beiträge aktualisieren...
remi
13.05.2009 10:56
This xmlrpc_encode function doesn't generate the "methodResponse" tag required by some clients.

Using xmlrpc_encode_request seems better:

<?php
$xmlresponse
= xmlrpc_encode_request(NULL, $phpresponse);

xmlrpc_encode(1) generate
<?xml version="1.0" encoding="utf-8"?>
<params>
<param>
 <value>
  <int>1</int>
 </value>
</param>
</params>

xmlrpc_encode_request(NULL,1) generate
<?xml version="1.0" encoding="iso-8859-1"?>
<methodResponse>
<params>
 <param>
  <value>
   <int>1</int>
  </value>
 </param>
</params>
</methodResponse>
?>
dave dot wilcock at gmail dot com
21.09.2007 16:05
Worth noting, you can use this function to generate fault response xml.

If you pass a pre-determined array into this function, as follows

<?
$myArray
= array ("faultCode"=>1,"faultString"=>"Too many params");
$xml = xmlrpc_encode($myArray);
?>

... you will get output much like the following:

<?xml version="1.0" encoding="utf-8"?>
<fault>
<value>
 <struct>
  <member>
   <name>faultCode</name>
   <value>
    <int>4</int>
   </value>
  </member>
  <member>
   <name>faultString</name>
   <value>
    <string>Too many params</string>
   </value>
  </member>
 </struct>
</value>
</fault>

All that is needed to do here to make it valid XML-RPC is inject some of your own methodResponse tags.

Check the XML-RPC specification at http://www.xmlrpc.com/spec

8.06.2006 16:32
Another quirk (more of a bug i think) is the automatic encoding of hashes where all keys that begin with a digit between 1 and 9 are lost:
http://bugs.php.net/bug.php?id=37746

Luckily I found a quick workaround. Just append chr(0x00) to your keys and the xmlrpc response will be correct. Maybe this will apply to encoding of hashes for requests too.
Amir.LaherATcomplinetDOTcom
26.08.2005 10:54
Beware this quirk: when you xmlrpc_encode a hash into xmlrpc, numeric keys will not be kept. Even strings containing numbers-only will be lost.

i.e.: xmlrpc_decode(xmlrpc_encode(array('123'=>456)));
returns: array(0=>456);

Apparently this is intentional functionality (see http://bugs.php.net/bug.php?id=21949)

2 workarounds:
1. prepend/append a string (even whitespace) to all keys, and strip them out at the other end. eg array('key_123'=>456);
2. create an array of hashes. array(array('key'=>123,'value'=>456));

I favour the second option because it's more deliberate.

... this also applies to xmlrpc_encode_request()
hfuecks at pinkgoblin dot com
15.08.2002 17:16
This function would be used by an XML-RPC server to convert PHP variables into an XML-RPC response.

It would be used within the PHP functions defined using xmlrpc_server_register_method()

Uses PHP variable reflection to assign the correct data types for the XML-RPC response. For example;

<?php
$params
= array ( "one"=>"red","two"=>"blue","three"=>"green" );

$response = xmlrpc_encode ( $params );

echo (
$response );
?>

Produces;

<?xml version='1.0' encoding="utf-8" ?>
<params>
<param>
 <value>
  <struct>
   <member>
    <name>one</name>
    <value>
     <string>red</string>
    </value>
   </member>
   <member>
    <name>two</name>
    <value>
     <string>blue</string>
    </value>
   </member>
   <member>
    <name>three</name>
    <value>
     <string>green</string>
    </value>
   </member>
  </struct>
 </value>
</param>
</params>

While
<?php
$params
= array ( "red", "blue", "green" );

$response = xmlrpc_encode ( $params );

echo (
$response );
?>

produces;

<?xml version='1.0' encoding="utf-8" ?>
<params>
<param>
 <value>
  <array>
   <data>
    <value>
     <string>red</string>
    </value>
    <value>
     <string>blue</string>
    </value>
    <value>
     <string>green</string>
    </value>
   </data>
  </array>
 </value>
</param>
</params>

And

<?php
$params
= 1;

$response = xmlrpc_encode ( $params );

echo (
$response );
?>

produces

<?xml version='1.0' encoding="utf-8" ?>
<params>
<param>
 <value>
  <int>1</int>
 </value>
</param>
</params>

It's useful to be aware of the settype() function (http://www.php.net/settype).

For binary base64 data and XML-RPC iso8601 date times the xmlrpc_set_type() function should be used on a PHP variable before using xmlrpc_encode()



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)