CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • REST API returns outdated values
Pages: [1]

Author Topic: REST API returns outdated values  (Read 671 times)

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
REST API returns outdated values
March 05, 2015, 06:20:03 am
Every time a contact has been edited, a third party app should be notified about that. To accomplish this, I'm posting to the third party app (via simple file_get_contents) inside a civicrm_post hook.
After the third party app has been notified, it is requesting the current contact details including the updated values via REST API. So much for the theory.

I have noticed, that the requested values are outdated, when they are request via REST API immediately after the civicrm_post hook has been triggered. Here is my test code to reproduce this weird behavior:

Code: [Select]
<?php 

/**
 * This hook is called after a db write on some core objects (e.g. contact)
 */
function myhook_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {

if(($objectName != 'Individual') && ($op != 'view')){

$contactId = $objectId;

$postdata = http_build_query(
array(
'api_key' => 'mysupersecretapikey',
'key' => 'mysupersecretsitekey',
'json' => 1,
'version' => 3,
'entity' => 'Contact',
'action' => 'get',
'contact_id' => $contactId
)
);

$opts = array('http' =>
array(
'method'  => 'POST',
'header'  => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);

$context  = stream_context_create($opts);

$restResult = file_get_contents('http://example.com/sites/all/modules/civicrm/extern/rest.php', false, $context);

watchdog('Contact Details via REST', '<pre>'. print_r($restResult, TRUE) .'</pre>'); 
// outputs old contact details with unchanged values -> BAD

try{
$params = array(
'version' => 3,
'id' => $contactId
);
$apiResult = civicrm_api('Contact', 'get', $params);

watchdog('Contact Details via civicrm_api', '<pre>'. print_r($apiResult, TRUE) .'</pre>');
// outputs new contact details with changed values -> GOOD
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
}

}

}

?>

It seems to be a timing, thread or cache issue but I have no idea how to find the cause.
« Last Edit: March 05, 2015, 06:22:02 am by PhilippMikael »

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: REST API returns outdated values
March 05, 2015, 06:24:24 am
Hmm, maybe there's some layer of caching going on in your webserver?
Out of curiosity, what's the reason for having the 3rd party app pull the data using rest. Can you push the data to them as well as just pinging them during your hook?
Try asking your question on the new CiviCRM help site.

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: REST API returns outdated values
March 05, 2015, 06:34:49 am
By pulling the data the 3rd party app can decide which contact details/fields should be returned. And besides that I don't have control over the 3rd party app. long story  ::)

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: REST API returns outdated values
March 09, 2015, 02:28:24 am
I'm using an apache, where mod_cache is enabled. But cache-control and pragma of the response header are set to no-cache. So I don't think that's a cache issue.

I' m pretty sure its a thread problem: When the data is requested via http (different thread), the transaction of the contact changes has not been committed. At this time only civicrm_api returns up-to-date data, because it's the same thread.
« Last Edit: March 09, 2015, 04:28:49 am by PhilippMikael »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • REST API returns outdated values

This forum was archived on 2017-11-26.