DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple XML-RPC In PHP (using CURL)

  • submit to reddit
        Ping Technorati using cURL and XMLRPC extensions.

# Using the XML-RPC extension to format the XML package
$request = xmlrpc_encode_request("", array("Copenhagen Ruby Brigade", "") );

# Using the cURL extension to send it off, 
# first creating a custom header block
$header[] = "Host:";
$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($request) . "\r\n";
$header[] = $request;

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, ""); # URL to post to
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header ); # custom headers, see above
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); # This POST is special, and uses its specified Content-type
$result = curl_exec( $ch ); # run!

echo $result;


Snippets Manager replied on Wed, 2008/02/06 - 12:37am

also, can i request for a workaround on the xmlrpc_encode_request function, because my server doesn't have the XML-RPC extension to its PHP installation. Thanks again!

Snippets Manager replied on Wed, 2008/02/06 - 12:37am

this code returns a "400 Bad Request".. how could i fix it? thanks for the help!