initial version of fulcrm_apiv2
This commit is contained in:
106
fulcrm_apiv2.module
Normal file
106
fulcrm_apiv2.module
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
function fulcrm_apiv2_permission() {
|
||||
return array( 'administer fulcrm APIv2' => array( 'title' => t('Administer fulcrm APIv2'),
|
||||
'description' => t('Perform main installation/administration tasks for fulcrm APIv2.'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function fulcrm_apiv2_menu() {
|
||||
/*
|
||||
$items[ 'admin/config/services/fulcrm' ] = array( 'page callback' => 'system_admin_menu_block_page',
|
||||
'file' => 'system.admin.inc',
|
||||
'file path' => drupal_get_path('module', 'system'),
|
||||
'title' => 'fulcrm',
|
||||
'description' => 'Configuration integration with fulcrm.org.',
|
||||
'position' => 'right',
|
||||
'access callback' => 'user_access',
|
||||
'access arguments' => array('access administration pages'),
|
||||
);
|
||||
*/
|
||||
$items[ 'admin/config/services/fulcrm/apiv2' ] = array( 'page callback' => 'fulcrm_apiv2_admin',
|
||||
'file' => 'fulcrm_apiv2.admin.inc',
|
||||
'title' => 'fulcrm APIv2',
|
||||
'description' => 'Credentials and settings for interacting with fulcrm.org APIv2.',
|
||||
'access callback' => 'user_access',
|
||||
'access arguments' => array('administer fulcrm APIv2'),
|
||||
);
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
function fulcrm_apiv2_http( $method, $object, $data = null ) {
|
||||
$curl = curl_init( 'https://fulcrm.org' . $object );
|
||||
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $method );
|
||||
|
||||
$request_headers = array( 'Authorization: ApiKey ' . variable_get( 'fulcrm_apiv2_user' ) . ':' . variable_get( 'fulcrm_apiv2_key' ),
|
||||
);
|
||||
if ( $data !== null ) {
|
||||
if ( is_array( $data ) ) {
|
||||
$data = json_encode( $data );
|
||||
$request_headers[] = 'Content-Type: application/json';
|
||||
}
|
||||
curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
|
||||
$request_headers[] = 'Content-Length: ' . strlen( $data );
|
||||
}
|
||||
|
||||
$request_headers[] = 'X-fulcrm-Client-ID: ' . variable_get( 'fulcrm_apiv2_client_id', $GLOBALS[ 'base_url' ] );
|
||||
|
||||
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
|
||||
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
|
||||
curl_setopt( $curl, CURLOPT_HEADER, true );
|
||||
curl_setopt( $curl, CURLOPT_HTTPHEADER, $request_headers );
|
||||
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
|
||||
curl_setopt( $curl, CURLOPT_TCP_NODELAY, true );
|
||||
|
||||
$result = curl_exec( $curl );
|
||||
$response = curl_getinfo( $curl );
|
||||
$header_size = curl_getinfo( $curl, CURLINFO_HEADER_SIZE );
|
||||
$header = substr( $result, 0, $header_size );
|
||||
$body = substr( $result, $header_size );
|
||||
$headers = array();
|
||||
foreach ( preg_split( '/[\r\n]+/', $header ) as $headerline ) {
|
||||
if ( preg_match( '/([-A-Za-z0-9_]+):\s*(.*)/', $headerline, $matches ) )
|
||||
$headers[ $matches[ 1 ] ] = $matches[ 2 ];
|
||||
}
|
||||
|
||||
$r = array( 'headers' => $headers,
|
||||
'body' => $body,
|
||||
'code' => $response[ 'http_code' ],
|
||||
);
|
||||
|
||||
if ( $response[ 'http_code' ] == 200 ) {
|
||||
$r[ 'success' ] = true; // GET
|
||||
} else if ( $response[ 'http_code' ] == 201 ) {
|
||||
$r[ 'success' ] = true; // POST
|
||||
} else if ( $response[ 'http_code' ] == 202 ) {
|
||||
$r[ 'success' ] = true; // PATCH
|
||||
} else if ( $response[ 'http_code' ] == 204 ) {
|
||||
$r[ 'success' ] = true; // DELETE
|
||||
} else {
|
||||
$r[ 'success' ] = false;
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
function fulcrm_apiv2_GET( $object ) {
|
||||
return fulcrm_apiv2_http( 'GET', $object );
|
||||
}
|
||||
|
||||
function fulcrm_apiv2_POST( $object, $data ) {
|
||||
return fulcrm_apiv2_http( 'POST', $object, $data );
|
||||
}
|
||||
|
||||
function fulcrm_apiv2_PUT( $object, $data ) {
|
||||
return fulcrm_apiv2_http( 'PUT', $object, $data );
|
||||
}
|
||||
|
||||
function fulcrm_apiv2_PATCH( $object, $data ) {
|
||||
return fulcrm_apiv2_http( 'PATCH', $object, $data );
|
||||
}
|
||||
|
||||
function fulcrm_apiv2_DELETE( $object ) {
|
||||
return fulcrm_apiv2_http( 'DELETE', $object );
|
||||
}
|
Reference in New Issue
Block a user