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) »
  • using API to create contacts, problems with utf-8 characters
Pages: [1]

Author Topic: using API to create contacts, problems with utf-8 characters  (Read 1189 times)

robbrandt

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 0
  • CiviCRM version: 4.0.7
  • CMS version: Drupal
  • MySQL version: 5.1.41-3ubuntu12.9
  • PHP version: 5.3.6
using API to create contacts, problems with utf-8 characters
July 25, 2014, 09:08:58 pm
I am using the API to create new contacts from another external database.  I am using v4.5 from git.

I have my create working for the most part, but it is failing on multibyte characters. I have tried it with both urlencoded values and unencoded. I have tried various sorts of Content Types, although I see docs verifying that values passed as an array should be treated as multipart/form-data.  When I try the "create", the returned message is:

{"is_error":1,"0":"error_message","1":"Unable to decode supplied JSON."}

Everything works great for non-multibyte values.

 Here's my code:

Code: [Select]
$query = 'http://localhost/civicrmlocal/sites/all/modules/civicrm/extern/rest.php"
           $curl = curl_init($query);
           
           $curl_post_data = array(
                   'entity' => 'Contact',
                   'action' => 'create',
                   'json' => '{
           "sequential":1,"contact_type":"Individual","user_unique_id":"'.$values[$newid]['openid_name'].'","first_name":"'.$first.'","last_name":"'.$last.'","middle_name":"'.$middle.'","nick_name":"'.$nick.'","display_name":"'.$values[$newid]['user_name'].'","email":"'.$values[$newid]['user_email'].'"}',
                   'api_key' => 'abunchofcharacters',
                   'key' => 'anotherbunchofchracters'
           );
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
           curl_setopt($curl, CURLOPT_POST, true);
           curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
           curl_setopt($curl,CURLOPT_HTTPHEADER,array (
                "Content-Type: multipart/form-data; charset=utf-8"
           ));
           $curl_response = curl_exec($curl);
           curl_close($curl);

sushant

  • I post frequently
  • ***
  • Posts: 126
  • Karma: 8
  • CiviCRM version: v3.2.x,v3.3.x v4.0.x, v4.x,trunk
  • CMS version: Drupal 6/7 ,joomla 1.5,1.6
  • MySQL version: 5.1.51
  • PHP version: 5.3.3
Re: using API to create contacts, problems with utf-8 characters
July 25, 2014, 10:31:47 pm
IMO there  may be  issue with JSON value your sending. You can try with JSON value like  below
Code: [Select]
'[{ "sequential":1,"contact_type":"Individual","user_unique_id":"'.$values[$newid]['openid_name'].'","first_name":"'.$first.'","last_name":"'.$last.'","middle_name":"'.$middle.'","nick_name":"'.$nick.'","display_name":"'.$values[$newid]['user_name'].'","email":"'.$values[$newid]['user_email'].'"}]'

Hth
CiviCRM Priority Support
http://support.civigardens.com/
http://osseed.com

robbrandt

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 0
  • CiviCRM version: 4.0.7
  • CMS version: Drupal
  • MySQL version: 5.1.41-3ubuntu12.9
  • PHP version: 5.3.6
Re: using API to create contacts, problems with utf-8 characters
July 26, 2014, 08:21:42 am
Thanks.  I got it working.  It was really hard to see what difference you specified in your version and I'm not sure this is what you meant, but there was a linefeed in mine after the opening { that was causing the problem.  Prior to that, the only difference I saw in yours was that the json was enclosed in square brackets.  I tried that too but it didn't help.

sushant

  • I post frequently
  • ***
  • Posts: 126
  • Karma: 8
  • CiviCRM version: v3.2.x,v3.3.x v4.0.x, v4.x,trunk
  • CMS version: Drupal 6/7 ,joomla 1.5,1.6
  • MySQL version: 5.1.51
  • PHP version: 5.3.3
Re: using API to create contacts, problems with utf-8 characters
July 26, 2014, 09:25:23 pm
That's good you got it working, also good way to validate your JSON is with http://jsonlint.com/
CiviCRM Priority Support
http://support.civigardens.com/
http://osseed.com

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • using API to create contacts, problems with utf-8 characters

This forum was archived on 2017-11-26.