The previous page defined the geo-nick API of the PHP-GTK.eu site. Here is a small example of how to use it from a client application, using either the CURL library, or native PHP coding to send the request and retrieve the XML-RPC results from the site.
<?php
if (!class_exists('gtk'))
{
die("Please load the php-gtk2 module in your php.ini\r\n");
}
/**
* This shows how to invoke an XML-RPC service using CURL
*/
function do_call_curl($host, $port, $backend, $request)
{
$url = "http://$host:$port/$backend";
$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($request);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$data = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
}
else
{
curl_close($ch);
return $data;
}
}
/**
* This shows how to invoke an XML-RPC service
* using stream wrappers, without CURL
*/
function do_call_raw($host, $port, $backend, $request)
{
$url = "http://$host:$port/$backend";
$params = array
(
'http' => array
(
'method' => 'POST',
'content' => $request,
'header' => array
(
'Content-type' => 'text/xml',
'Content-length' => strlen($request),
),
)
);
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp)
{
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false)
{
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
/**
* Prepare the request data
*/
$host = 'www.php-gtk.eu';
$port = 80;
$backend = 'xmlrpc.php';
/**
* encode them to XML
*/
$request = xmlrpc_encode_request('gtkphpnet.geo_nick', '', 10);
/**
* Send the request: we could just as well use do_call_curl
* with the same parameters
*/
$response = do_call_raw($host, $port, $backend, $request);
/**
* Now parse the results to native PHP
*/
$res = xmlrpc_decode($response);
/**
* and display them in a PHP-GTK2 window
*/
$window = new GtkWindow();
$window->set_title('www.php-gtk.eu geo-nick service');
$window->connect_simple('destroy', array('Gtk','main_quit'));
$textBuffer = new GtkTextBuffer();
$textBuffer->set_text(print_r($res, true));
$textView = new GtkTextView();
$textView->set_buffer($textBuffer);
$textView->set_editable(false);
$window->add($textView);
$window->show_all();
Gtk::main();
?>If your configuration does not include the standard XML-RPC extension, you can still use the Pear XML-RPC, as in the Accessing geo-nick without native XML-RPC example. Although arguably slower, the Pear XML-RPC library has frees you from the issue of sending/retrieving the XML messages.
Recent comments
47 weeks 2 days ago
48 weeks 2 days ago
48 weeks 5 days ago
51 weeks 2 days ago
1 year 19 weeks ago
1 year 23 weeks ago
1 year 27 weeks ago
1 year 27 weeks ago
1 year 27 weeks ago
1 year 44 weeks ago