Skip to content

Instantly share code, notes, and snippets.

@sohelrana820
Created September 17, 2018 10:55
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 sohelrana820/32f5c55f2876c61c27ec027c6e477d8d to your computer and use it in GitHub Desktop.
Save sohelrana820/32f5c55f2876c61c27ec027c6e477d8d to your computer and use it in GitHub Desktop.
XML RPC Client Example
<?php
/**
* Class XMLRPCClient
*/
class XMLRPCClient
{
/**
* @var
*/
private $url;
/**
* XMLRPCClient constructor.
* @param $url
*/
function __construct($url)
{
$this->url = $url;
}
/**
* @return mixed
*/
public function call()
{
ini_set("display_error", 1);
error_reporting(E_ALL);
// get arguments
$params = func_get_args();
$method = array_shift($params);
$encodedRequest = xmlrpc_encode_request($method, $params);
// set URL and other appropriate options
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedRequest);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// issue the request
$response = curl_exec($ch);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlErrorNo = curl_errno($ch);
$curlError = curl_error($ch);
curl_close($ch);
// check for curl errors
if ($curlErrorNo != 0) {
die("Curl ERROR: {$curlErrorNo} - {$curlError}n");
}
// check for server errors
if ($responseCode != 200) {
die("ERROR: non-200 response from server: {$responseCode} - {$response}n");
}
return xmlrpc_decode($response);
}
}
$client = new XMLRPCClient("http://example.com");
$response = $client->call('mark.getUserMark', array(1, "{{username}}", "{{password}}"));
var_dump($response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment