Using Harvest php API http://mdbitz.com/harvest-api/examples/ and my harvest php array prints following data:
$myresult = $harvestAPI->getUser($client_id);
$data = $myresult->get( "data" );
print_r($data);
data:
Harvest_User Object ( [_root:protected]
=> user [_convert:protected]
=> 1 [_values:protected]
=> Array ( [id]
=> 999 [email]
=> user@example.com [created-at]
=> 2014-06-12T20:00:00Z [is-admin]
=> false [first-name]
=> John [last-name]
=> Smith [timezone]
=> Mountain Time (US & Canada) [is-contractor]
=> false [telephone]
=> [is-active]
=> true [has-access-to-all-future-projects]
=> false [default-hourly-rate]
=> 200.0 [department]
=> Development [dev]
=> false [updated-at]
=> 2015-06-15T18:00:00Z [cost-rate]
=> 100.0 ) )
but when using json class to conver it to json, it gives me empty {} any idea whats happening ?
$dataJSON = json_encode($data);
print_r($dataJSON);
It does not work because all the properties are protected. While print_r
is a special debugging function and can display protected properties, json_encode
can only read public properties. As your Harvest_User
object doesn't have any public properties, the JSON object is empty.
The Harvest_User
class inherits from Harvest_Abstract
, which again implements the magic __get
and __set
methods. Unfortunately, json_encode
cannot use them, because it doesn't even know which variables to look for.
To solve your problem, you can write a helper class to transform your Harvest objects into plain PHP objects and then encode them to JSON.
You might also want to create a bug report for Harvest and ask them to implement Serializable
and/or JsonSerializable
.