1 /* A simple news-searcher, written in C to demonstrate how to use the
2 xmplrpc-c client library.
4 This program connects to an XMLRPC server that O'Reilly runs on the
5 Internet, gets some information, and displays it on Standard Output.
7 Note that that server is not in any way designed specifically for xmlrpc-c.
8 It simply implements the XMLRPC protocol, and works with any client that
11 The service that the aforementioned server provides is that it gives you
12 a list of news articles that match a certain regular expression. You give
13 that regular expression an argument to this client program.
15 For more details about O'Reilly's excellent Meerkat news service, see:
16 http://www.oreillynet.com/pub/a/rss/2000/11/14/meerkat_xmlrpc.html
23 #include <xmlrpc-c/base.h>
24 #include <xmlrpc-c/client.h>
26 #include "config.h" /* information about this build environment */
28 #define NAME "XML-RPC C Meerkat Query Demo"
30 #define MEERKAT_URL "http://www.oreillynet.com/meerkat/xml-rpc/server.php"
33 const char * searchArg;
39 parseCommandLine(int const argc,
40 const char ** const argv,
41 struct cmdline * const cmdlineP) {
44 fprintf(stderr, "Need at least one argument: "
45 "A mysql regular expression "
46 "search pattern. Try 'query-meerkat Linux'\n");
49 cmdlineP->searchArg = argv[1];
54 cmdlineP->hours = atoi(argv[2]);
55 if (cmdlineP->hours > 49) {
56 fprintf(stderr, "It's not nice to ask for > 49 hours "
61 fprintf(stderr, "There are at most 2 arguments: "
63 "and number of hours.");
73 die_if_fault_occurred(xmlrpc_env * const env) {
74 /* We're a command-line utility, so we abort if an error occurs. */
75 if (env->fault_occurred) {
76 fprintf(stderr, "XML-RPC Fault #%d: %s\n",
77 env->fault_code, env->fault_string);
84 /* Hey! We fit in one function. */
87 const char** const argv) {
89 struct cmdline cmdline;
92 xmlrpc_value *stories, *story;
96 parseCommandLine(argc, argv, &cmdline);
98 snprintf(time_period, sizeof(time_period), "%dHOUR", cmdline.hours);
100 xmlrpc_env_init(&env);
102 /* Set up our client. */
103 xmlrpc_client_init2(&env, XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION, NULL, 0);
105 die_if_fault_occurred(&env);
107 /* Ask Meerkat to look for matching stories. */
108 stories = xmlrpc_client_call(&env, MEERKAT_URL,
109 "meerkat.getItems", "({s:s,s:i,s:s})",
110 "search", cmdline.searchArg,
111 "descriptions", (xmlrpc_int32) 76,
112 "time_period", time_period);
113 die_if_fault_occurred(&env);
115 /* Loop over the stories. */
116 size = xmlrpc_array_size(&env, stories);
117 die_if_fault_occurred(&env);
119 for (i = 0; i < size; i++) {
122 const char * description;
124 /* Extract the useful information from our story. */
125 story = xmlrpc_array_get_item(&env, stories, i);
126 die_if_fault_occurred(&env);
127 xmlrpc_decompose_value(&env, story, "{s:s,s:s,s:s,*}",
130 "description", &description);
131 die_if_fault_occurred(&env);
133 /* Print a separator line if necessary. */
139 /* Print the story. */
140 if (strlen(description) > 0) {
141 printf("%s\n%s\n%s\n", title, description, link);
143 printf("%s\n%s\n", title, link);
147 free((char*)description);
150 /* Shut down our client. */
151 xmlrpc_DECREF(stories);
152 xmlrpc_env_clean(&env);
153 xmlrpc_client_cleanup();