RE: JSON RPC with SSL and .pem certificates? [SOLVED]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Daevid Vincent [mailto:daevid@xxxxxxxxxx] 
> Sent: Thursday, May 27, 2010 4:48 PM
> To: 'php-general@xxxxxxxxxxxxx'
> Subject: JSON RPC with SSL and .pem certificates?
> 
> Anyone have a good example or routine or library or something 
> that will use JSON-RPC client and SSL .pem certs? All the 
> code I've found out there is pretty dated or unkept or lacks 
> one or more of those requirements.
> 
> http://jsonrpcphp.org/ is about as close as I could find, but 
> it doesn't use SSL and hasn't been touched since 2007.

Well, I just hacked together some cURL and this is working pretty good for
our needs. Maybe it will help someone else. YMMV.

#!/usr/bin/php -q
<?php
	error_reporting(E_ALL & ~E_NOTICE);

###########################################################
############## USER DEFINED PARAMETERS ####################
###########################################################
	define('JSONRPC_SERVER', "https://IP_ADDRESS:PORT";);
###########################################################
###########################################################
###########################################################

	$OPTION['debug'] = false;

	//loop through our arguments and see what the user selected
    for ($i = 1; $i < $_SERVER["argc"]; $i++)
    {
        switch($_SERVER["argv"][$i])
        {
            case "-v":
            case "--version":
				echo  $_SERVER['argv'][0]." v2010-05-28
02:15 PM\n";
				exit;
                break;

            case "--debug":
                $OPTION['debug'] = true;
                break;

            case "-?":
            case "-h":
            case "--help":
?>
  Usage: <?php echo $_SERVER['argv'][0]; ?> <option>

  --help, -help, -h, or -? options, to get this help.
  --version to return the version of this file.
  --debug   to turn on output debugging.
<?php
                exit;
                break;
        }
    } //parse arguments

	$params = array('foo'=>"bar",'myid'=>"69");
	$result = cURL_POST_JSON('some_method', $params);
	var_dump($result);

exit("\n");

###########################################################################
###########################################
########  F U N C T I O N S
###########################################################################
###############
###########################################################################
###########################################

/**
* Send a POST JSON-RPC method posting to a URL using cURL libraries.
*
* @param    string $method the remote method to call
* @param	string $parameters the data to send in "?key=value&" format
* @access	public
* @return	array or false
* @date    	2010-05-27
*/
function cURL_POST_JSON($method, $parameters)
{
	global $OPTION;

	$url = JSONRPC_SERVER;

	if ($OPTION['debug']) echo "\nURL = ".$url."\n";
	if ($OPTION['debug']) echo "\nmethod = ".$method."\n";
	if ($OPTION['debug']) { echo "\nparameters = \n";
var_dump($parameters); }

	$request = array(
					'method' => $method,
					'params' => $parameters,
					'id' => md5(date('c'))
					);
	$request = json_encode($request);
	if ($OPTION['debug']) echo "\nrequest = ".$request."\n";

	$PASS = false;
	// create a new curl resource
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);

	curl_setopt($ch, CURLOPT_SSLCERT, './YOUR_Certificate.pem');
	curl_setopt($ch, CURLOPT_SSLCERTTYPE, "PEM");
	curl_setopt($ch, CURLOPT_SSLKEY, './YOUR_Private.pem');
	curl_setopt($ch, CURLOPT_SSLKEYTYPE, "PEM");
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
	//VERIFYPEER is false because otherwise we get this:
	//cURL error: SSL certificate problem, verify that the CA cert is
OK. Details:
	//error:14090086:SSL
routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:
application/json'));
	curl_setopt($ch, CURLOPT_HEADER, false); //FALSE to exclude the
header from the output (otherwise it screws up json_decode)
	curl_setopt($ch, CURLOPT_NOBODY, false); //FALSE because we want
the body too
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // get the response
as a string from curl_exec(), rather than echoing it
	curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); // don't use a
cached version of the url
	//curl_setopt($ch, CURLOPT_TIMEOUT, 4);
	$returnData = curl_exec($ch);
	if (curl_errno($ch)) print "\n\ncURL error:
".curl_error($ch)."\n\n";
	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);

	if ( $httpcode == "200" )
	{
		$PASS = true;
	}

	if ($OPTION['debug']) echo "\nDART API JSON-RPC POST method ".(
($PASS) ? "SUCCESSFULL" : "FAILED" ).".\n\n";

	if ($PASS)
		return json_decode($returnData,true);
		//return $returnData;
	else
		exit();
}
?>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux