call APIv2

master
Marek Isalski 9 years ago
parent 38a94bac3f
commit fc5a6c6be3

@ -30,8 +30,39 @@ function fulcrm_apiv2_menu() {
return $items; return $items;
} }
function fulcrm_apiv2_http( $method, $object, $data = null ) { function fulcrm_apiv2_http( $method, $object = null, $data = null, $query = null ) {
$curl = curl_init( 'https://fulcrm.org' . $object ); $url = null;
if ( substr( $object, 0, 8 ) === 'http://' ) {
throw new Exception( "cannot use HTTP on fulcrm APIv2" );
} elseif ( substr( $object, 0, 8 ) === 'https://' ) {
$host = parse_url( $object, PHP_URL_HOST );
if ( ( $host === 'fulcrm.org' ) || ( substr( $host, -10 ) === 'fulcrm.org' ) ) {
$object = parse_url( $object, PHP_URL_PATH );
$url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . $object;
} else
throw new Exception( "cannot point fulcrm APIv2 away from fulcrm.org" );
} elseif ( substr( $object, 0, 8 ) === '/api/v2/' ) {
$url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . $object;
} else {
$url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . '/api/v2/' . $object;
}
if ( is_null( $object ) && is_array( $data ) ) {
if ( array_key_exists( 'url', $data ) ) {
if ( is_null( $url ) )
$url = $data[ 'url' ];
elseif ( $data[ 'url' ] !== $url )
throw new Exception( '$object URL specified different from $data["url"]' );
}
}
if ( is_array( $query ) )
$url = $url . '?' . http_build_query( $query );
elseif ( is_string( $query ) )
$url = $url . '?' . $query;
$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $method ); curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $method );
$request_headers = array( 'Authorization: ApiKey ' . variable_get( 'fulcrm_apiv2_user' ) . ':' . variable_get( 'fulcrm_apiv2_key' ), $request_headers = array( 'Authorization: ApiKey ' . variable_get( 'fulcrm_apiv2_user' ) . ':' . variable_get( 'fulcrm_apiv2_key' ),
@ -82,25 +113,28 @@ function fulcrm_apiv2_http( $method, $object, $data = null ) {
$r[ 'success' ] = false; $r[ 'success' ] = false;
} }
$r[ 'data' ] = json_decode( $body, $assoc = true );
$r[ 'data_error' ] = json_last_error();
return $r; return $r;
} }
function fulcrm_apiv2_GET( $object ) { function fulcrm_apiv2_GET( $object, $query = null ) {
return fulcrm_apiv2_http( 'GET', $object ); return fulcrm_apiv2_http( 'GET', $object, $data = null, $query = $query );
} }
function fulcrm_apiv2_POST( $object, $data ) { function fulcrm_apiv2_POST( $object, $data, $query = null ) {
return fulcrm_apiv2_http( 'POST', $object, $data ); return fulcrm_apiv2_http( 'POST', $object, $data = $data, $query = $query );
} }
function fulcrm_apiv2_PUT( $object, $data ) { function fulcrm_apiv2_PUT( $object, $data, $query = null ) {
return fulcrm_apiv2_http( 'PUT', $object, $data ); return fulcrm_apiv2_http( 'PUT', $object, $data = $data, $query = $query );
} }
function fulcrm_apiv2_PATCH( $object, $data ) { function fulcrm_apiv2_PATCH( $object, $data, $query = null ) {
return fulcrm_apiv2_http( 'PATCH', $object, $data ); return fulcrm_apiv2_http( 'PATCH', $object, $data = $data, $query = $query );
} }
function fulcrm_apiv2_DELETE( $object ) { function fulcrm_apiv2_DELETE( $object, $query = null ) {
return fulcrm_apiv2_http( 'DELETE', $object ); return fulcrm_apiv2_http( 'DELETE', $object, $data = null, $query = $query );
} }

Loading…
Cancel
Save