/php/janrain-engage-php-sdk/token/index.php

https://github.com/LeifW/Janrain-Sample-Code · PHP · 91 lines · 42 code · 8 blank · 41 comment · 3 complexity · 7566844319b6adf0003b5c7726f1b8e8 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright 2011
  4. * Janrain Inc.
  5. * All rights reserved.
  6. */
  7. /**
  8. * Below is a very simple PHP 5 script that
  9. * implements an Engage token URL to collect
  10. * and output the results from auth_info.
  11. * The code below assumes you have the
  12. * CURL HTTP fetching library with SSL and
  13. * PHP JSON support.
  14. */
  15. ob_start();
  16. require_once('../library/engage.auth.lib.php');
  17. $debug_array = array('Debug out:');
  18. /**
  19. * For a production script it would be better
  20. * to include (require_once) the apiKey in
  21. * from a file outside the web root to
  22. * enhance security.
  23. *
  24. * Set your API key (secret) in this file.
  25. * The varable is $api_key
  26. */
  27. require_once('engage-conf.php');
  28. $token = $_POST['token'];
  29. $format = ENGAGE_FORMAT_JSON;
  30. $extended = $auth_info_extended;
  31. $result = engage_auth_info($api_key, $token, $format, $extended);
  32. if ($result === false) {
  33. $errors = engage_get_errors();
  34. foreach ($errors as $error=>$label) {
  35. $debug_array[] = 'Error: '.$error;
  36. }
  37. } else {
  38. /**
  39. * On a successful authentication store
  40. * the auth_info data in the variable
  41. * $auth_info_array
  42. */
  43. $array_out = true;
  44. $auth_info_array = engage_parse_result($result, $format, $array_out);
  45. //Put a printed copy in the debug.
  46. $debug_array[] = print_r($auth_info_array, true);
  47. /**
  48. * This is the point to add code to do something with the Engage data.
  49. */
  50. }
  51. $errors = engage_get_errors(ENGAGE_ELABEL_ERROR);
  52. foreach ($errors as $error=>$label) {
  53. $error_array[] = 'Error: '.$error;
  54. }
  55. /*
  56. * Uncomment lines below to get SDK level
  57. * debug data. Caution: This could result in
  58. * revealing the api_key.
  59. */
  60. //$debugs = engage_get_errors(ENGAGE_ELABEL_DEBUG);
  61. //foreach ($debugs as $debug=>$label) {
  62. // $debug_array[] = 'Debug: '.$debug;
  63. //}
  64. $the_buffer = ob_get_contents();
  65. if (!empty($the_buffer)) {
  66. $debug_array[] = 'Buffer: '.$the_buffer;
  67. }
  68. /* The variable (string) $the_debug will contain debug data. */
  69. $the_debug = implode("\n", $debug_array);
  70. $the_error = implode("\n", $error_array);
  71. ob_end_clean();
  72. ?>
  73. <html>
  74. <head>
  75. <title>Janrain Engage token URL example</title>
  76. </head>
  77. <body>
  78. <pre>
  79. <?php echo $the_error; ?>
  80. <?php echo $the_debug; ?>
  81. </pre>
  82. </body>
  83. </html>