Ejemplos de integración en PHP
Para poder utilizar estos ejemplo necesitamos disponer de credenciales para acceder a la capa de servicios rest.
En estos ejemplos hacemos uso de la librería oauth-php para gestionar la seguridad de acceso a los servicios con OAuth 1.0, puedes acceder a esta librería en https://code.google.com/archive/p/oauth-php/
require_once dirname(__FILE__) . '/library/OAuthRequestSigner.php';
define("DOCUMENTS_API_URL", "https://sandbox.viafirma.com/documents/api/v3");
define("DOCUMENTS_CONSUMER_KEY", "com.viafirma.documents.XXXXXXX");
define("DOCUMENTS_CONSUMER_SECRET", "XXXXXXXXXXXX");
Comprobar acceso a los servicios rest
function system_alive ()
{
error_reporting(E_ALL);
header('Content-Type: text/plain; charset=utf-8');
echo "Documents OAuth 1.0a Client\n\n";
echo "See also: http://doc.viafirma.com/documents\n\n";
$url=DOCUMENTS_API_URL."/system/alive";
echo "URL: ".$url."\n";
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
echo prettyPrint($result);
// Closing
curl_close($ch);
}
Enviar una nueva petición
function send_message ()
{
error_reporting(E_ALL);
header('Content-Type: text/plain; charset=utf-8');
$url=DOCUMENTS_API_URL."/messages";
echo "URL: ".$url."\n";
OAuthStore::instance('MySQL', array('conn'=>false));
$req = new OAuthRequestSigner($url, 'POST');
$fecha = new DateTime();
$secrets = array(
'consumer_key' => DOCUMENTS_CONSUMER_KEY,
'consumer_secret' => DOCUMENTS_CONSUMER_SECRET,
'token' => '',
'token_secret' => '',
'signature_methods' => array('HMAC-SHA1'),
'nonce' => '3jd834jd9',
'timestamp' => $fecha->getTimestamp(),
);
$req->sign(0, $secrets);
// POST
$string_json = file_get_contents("../message.json");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $string_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// OAuth Header
$headr = array();
$headr[] = 'Content-Length: ' . strlen($string_json);
$headr[] = 'Content-type: application/json';
$headr[] = ''.$req->getAuthorizationHeader();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
$result = curl_exec($ch);
echo "MessageCode: ".$result;
// Closing
curl_close($ch);
}
Recuperar información de una petición
function get_message ($messageCode = '')
{
error_reporting(E_ALL);
header('Content-Type: text/plain; charset=utf-8');
$url=DOCUMENTS_API_URL."/messages/".$messageCode;
echo "URL: ".$url."\n";
OAuthStore::instance('MySQL', array('conn'=>false));
$req = new OAuthRequestSigner($url, 'GET');
$fecha = new DateTime();
$secrets = array(
'consumer_key' => DOCUMENTS_CONSUMER_KEY,
'consumer_secret' => DOCUMENTS_CONSUMER_SECRET,
'token' => '',
'token_secret' => '',
'signature_methods' => array('HMAC-SHA1'),
'nonce' => '3jd834jd9',
'timestamp' => $fecha->getTimestamp(),
);
$req->sign(0, $secrets);
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// OAuth Header
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = ''.$req->getAuthorizationHeader();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
// Execute
$result=curl_exec($ch);
echo prettyPrint($result);
// Closing
curl_close($ch);
}
Recuperar un documento firmado
function download_signed ($messageCode = '')
{
error_reporting(E_ALL);
header('Content-Type: text/plain; charset=utf-8');
$url=DOCUMENTS_API_URL."/documents/download/signed/".$messageCode;
echo "URL: ".$url."\n";
OAuthStore::instance('MySQL', array('conn'=>false));
$req = new OAuthRequestSigner($url, 'GET');
$fecha = new DateTime();
$secrets = array(
'consumer_key' => DOCUMENTS_CONSUMER_KEY,
'consumer_secret' => DOCUMENTS_CONSUMER_SECRET,
'token' => '',
'token_secret' => '',
'signature_methods' => array('HMAC-SHA1'),
'nonce' => '3jd834jd9',
'timestamp' => $fecha->getTimestamp(),
);
$req->sign(0, $secrets);
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// OAuth Header
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = ''.$req->getAuthorizationHeader();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
// Execute
$result=curl_exec($ch);
echo prettyPrint($result);
// Closing
curl_close($ch);
}
Recuperar la lista de dispositivos de un usuario
function get_user_devices ($userCode = '')
{
error_reporting(E_ALL);
header('Content-Type: text/plain; charset=utf-8');
$url=DOCUMENTS_API_URL."/devices/user/".$userCode;
echo "URL: ".$url."\n";
OAuthStore::instance('MySQL', array('conn'=>false));
$req = new OAuthRequestSigner($url, 'GET');
$fecha = new DateTime();
$secrets = array(
'consumer_key' => DOCUMENTS_CONSUMER_KEY,
'consumer_secret' => DOCUMENTS_CONSUMER_SECRET,
'token' => '',
'token_secret' => '',
'signature_methods' => array('HMAC-SHA1'),
'nonce' => '3jd834jd9',
'timestamp' => $fecha->getTimestamp(),
);
$req->sign(0, $secrets);
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// OAuth Header
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = ''.$req->getAuthorizationHeader();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
// Execute
$result=curl_exec($ch);
echo prettyPrint($result);
// Closing
curl_close($ch);
}
Rechazar una petición
function reject_message ($messageCode = '', $comment = '')
{
error_reporting(E_ALL);
header('Content-Type: text/plain; charset=utf-8');
$url=DOCUMENTS_API_URL."/messages/reject/".$messageCode;
echo "URL: ".$url."\n";
$data = array(
'comment' => $comment,
);
$params=http_build_query($data);
OAuthStore::instance('MySQL', array('conn'=>false));
echo "URL: ".$url."?comment=".rawurlencode($comment);
$req = new OAuthRequestSigner($url."?comment=".rawurlencode($comment), 'PUT');
$fecha = new DateTime();
$secrets = array(
'consumer_key' => DOCUMENTS_CONSUMER_KEY,
'consumer_secret' => DOCUMENTS_CONSUMER_SECRET,
'token' => '',
'token_secret' => '',
'signature_methods' => array('HMAC-SHA1'),
'nonce' => '3jd834jd9',
'timestamp' => $fecha->getTimestamp(),
);
$req->sign(0, $secrets);
// PUT
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// OAuth Header
$headr = array();
$headr[] = 'Content-type: application/x-www-form-urlencoded';
$headr[] = ''.$req->getAuthorizationHeader();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
$result=curl_exec($ch);
echo prettyPrint($result);
// Closing
curl_close($ch);
}
Funciones de utilidad
function prettyPrint( $json )
{
$result = '';
$level = 0;
$in_quotes = false;
$in_escape = false;
$ends_line_level = NULL;
$json_length = strlen( $json );
for( $i = 0; $i < $json_length; $i++ ) {
$char = $json[$i];
$new_line_level = NULL;
$post = "";
if( $ends_line_level !== NULL ) {
$new_line_level = $ends_line_level;
$ends_line_level = NULL;
}
if ( $in_escape ) {
$in_escape = false;
} else if( $char === '"' ) {
$in_quotes = !$in_quotes;
} else if( ! $in_quotes ) {
switch( $char ) {
case '}': case ']':
$level--;
$ends_line_level = NULL;
$new_line_level = $level;
break;
case '{': case '[':
$level++;
case ',':
$ends_line_level = $level;
break;
case ':':
$post = " ";
break;
case " ": case "\t": case "\n": case "\r":
$char = "";
$ends_line_level = $new_line_level;
$new_line_level = NULL;
break;
}
} else if ( $char === '\\' ) {
$in_escape = true;
}
if( $new_line_level !== NULL ) {
$result .= "\n".str_repeat( "\t", $new_line_level );
}
$result .= $char.$post;
}
return $result;
}
results matching ""
No results matching ""