/plugin/PBAPI/PBAPI/Response/json.php

https://bitbucket.org/chamilo/chamilo-ext-repo-photobucket-dev/ · PHP · 72 lines · 28 code · 8 blank · 36 comment · 2 complexity · 39e74bc456c1201977334bce3687c293 MD5 · raw file

  1. <?php
  2. use common\libraries\Path;
  3. /**
  4. * Photobucket API
  5. * Fluent interface for PHP5
  6. * json response parser
  7. *
  8. * @author Photobucket
  9. * @package PBAPI
  10. * @subpackage Response
  11. *
  12. * @copyright Copyright Copyright (c) 2008, Photobucket, Inc.
  13. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  14. */
  15. /**
  16. * Load Response parent
  17. */
  18. require_once dirname(__FILE__) . '/../Response.php';
  19. /**
  20. * Response json format parser
  21. *
  22. * Requires either the JSON extension, or the Services_JSON class from PEAR
  23. *
  24. * @package PBAPI
  25. * @subpackage Response
  26. */
  27. class PBAPI_Response_json extends PBAPI_Response
  28. {
  29. /**
  30. * Do JSON parse
  31. *
  32. * @param string $response_string string to parse
  33. * @param bool $onlycontent only return content 'node'
  34. * @return array associative array of response data
  35. */
  36. public function parse($string, $onlycontent = false)
  37. {
  38. $result = array();
  39. if (function_exists('json_decode'))
  40. {
  41. $result = json_decode($string, true);
  42. }
  43. else
  44. {
  45. //compatibility using PEAR
  46. require_once ('Services/JSON.php');
  47. $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  48. $result = $json->decode($string);
  49. }
  50. $this->detectException($result);
  51. if ($onlycontent)
  52. return @$result['content'];
  53. return $result;
  54. }
  55. /**
  56. * Returns optimal format string for given parser
  57. *
  58. * @return string
  59. */
  60. public function getFormat()
  61. {
  62. return 'json';
  63. }
  64. }