XML RPC Client Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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