Skip to content

Instantly share code, notes, and snippets.

@sorbing
Created May 20, 2016 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorbing/266b41c8fab37d9d5fd67f8ebccbd0cb to your computer and use it in GitHub Desktop.
Save sorbing/266b41c8fab37d9d5fd67f8ebccbd0cb to your computer and use it in GitHub Desktop.
Example usage a WordPress XML-RPC API
<?php
// Require: php5-xmlrpc
// sudo apt-get install php5-xmlrpc
// @note For debug only
header('Content-Type: text/plain;');
$wpUrl = '';
$wpUser = '';
$wpPass = '';
function do_api_request_via_curl($url, $xmlRequest)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/xml']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
$error = curl_error($ch);
if ($error) {
die('Error: ' . $error);
}
$response = curl_exec($ch);
return $response;
}
function do_api_request_via_fgc($url, $xmlRequest)
{
$options = [
'http' => [
'method' => 'POST',
'header' => 'Content-Type: text/xml',
'content' => $xmlRequest
]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
$xmlRequest = xmlrpc_encode_request('mm.wpQuery', [1, $wpUser, $wpPass, [
'category_name' => 'reviews',
'numberposts' => 2
], ['ID', 'post_title']]);
//$xmlResponse = do_api_request_via_curl($wpUrl, $xmlRequest);
$xmlResponse = do_api_request_via_fgc($wpUrl, $xmlRequest);
$data = xmlrpc_decode($xmlResponse);
echo "<pre>"; print_r($data); echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment