Skip to content

Instantly share code, notes, and snippets.

@shuding
Last active September 23, 2018 22:04
Show Gist options
  • Save shuding/8fd50b1f732b87025762 to your computer and use it in GitHub Desktop.
Save shuding/8fd50b1f732b87025762 to your computer and use it in GitHub Desktop.
XMLRPC PHP Client Example
<?php
class XMLRPC_Client {
private $url;
function __construct( $url ) {
$this->url = $url;
}
/**
* Call the XML-RPC method named $method and return the results, or die trying!
*
* @param string $method XML-RPC method name
* @param mixed ... optional variable list of parameters to pass to XML-RPC call
*
* @return array result of XML-RPC call
*/
public function call() {
ini_set("display_error", 1);
error_reporting(E_ALL);
// get arguments
$params = func_get_args();
$method = array_shift( $params );
$post = xmlrpc_encode_request( $method, $params );
/*
$post = str_replace("\n", "", $post);
$post = str_replace(" ", "", $post);
echo $post;
*/
$ch = curl_init();
// set URL and other appropriate options
curl_setopt( $ch, CURLOPT_URL, $this->url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// issue the request
$response = curl_exec( $ch );
$response_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
$curl_errorno = curl_errno( $ch );
$curl_error = curl_error( $ch );
curl_close( $ch );
// check for curl errors
if ( $curl_errorno != 0 ) {
die( "Curl ERROR: {$curl_errorno} - {$curl_error}n" );
}
// check for server errors
if ( $response_code != 200 ) {
die( "ERROR: non-200 response from server: {$response_code} - {$response}n" );
}
// return $response;
// $response .= 'e>';
return xmlrpc_decode( $response );
}
}
$client = new XMLRPC_Client( "http://stu.fudan.edu.cn/dandu/xmlrpc.php" );
$response = $client->call( 'mark.getUserMark', array(1, "{{username}}", "{{password}}") );
print_r($response);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment