/globals.php

https://github.com/jtzl/oauth_twitter · PHP · 227 lines · 152 code · 35 blank · 40 comment · 29 complexity · b301f89497a5c2596abad4a122267c08 MD5 · raw file

  1. <?php
  2. /**
  3. * Globals and utilities for OAuth Examaples package
  4. */
  5. // Fill in the next two constants
  6. define('OAUTH_CONSUMER_KEY', 'XaWgJdTYPatqCXfkeAvRT');
  7. define('OAUTH_CONSUMER_SECRET', 'sfkljwerWDFS2fxSFrqsadfdaf43EREx2SafFADddw');
  8. $progname = $argv[0];
  9. $debug = 0; // Set to 1 for verbose debugging output
  10. function logit($msg,$preamble=true)
  11. {
  12. // date_default_timezone_set('America/Los_Angeles');
  13. $now = date(DateTime::ISO8601, time());
  14. error_log(($preamble ? "+++${now}:" : '') . $msg);
  15. }
  16. /**
  17. * Do an HTTP GET
  18. * @param string $url
  19. * @param int $port (optional)
  20. * @param array $headers an array of HTTP headers (optional)
  21. * @return array ($info, $header, $response) on success or empty array on error.
  22. */
  23. function do_get($url, $port=80, $headers=NULL)
  24. {
  25. $retarr = array(); // Return value
  26. $curl_opts = array(CURLOPT_URL => $url,
  27. CURLOPT_PORT => $port,
  28. CURLOPT_POST => false,
  29. CURLOPT_SSL_VERIFYHOST => false,
  30. CURLOPT_SSL_VERIFYPEER => false,
  31. CURLOPT_RETURNTRANSFER => true);
  32. if ($headers) { $curl_opts[CURLOPT_HTTPHEADER] = $headers; }
  33. $response = do_curl($curl_opts);
  34. if (! empty($response)) { $retarr = $response; }
  35. return $retarr;
  36. }
  37. /**
  38. * Do an HTTP POST
  39. * @param string $url
  40. * @param int $port (optional)
  41. * @param array $headers an array of HTTP headers (optional)
  42. * @return array ($info, $header, $response) on success or empty array on error.
  43. */
  44. function do_post($url, $postbody, $port=80, $headers=NULL)
  45. {
  46. $retarr = array(); // Return value
  47. $curl_opts = array(CURLOPT_URL => $url,
  48. CURLOPT_PORT => $port,
  49. CURLOPT_POST => true,
  50. CURLOPT_SSL_VERIFYHOST => false,
  51. CURLOPT_SSL_VERIFYPEER => false,
  52. CURLOPT_POSTFIELDS => $postbody,
  53. CURLOPT_RETURNTRANSFER => true);
  54. if ($headers) { $curl_opts[CURLOPT_HTTPHEADER] = $headers; }
  55. $response = do_curl($curl_opts);
  56. if (! empty($response)) { $retarr = $response; }
  57. return $retarr;
  58. }
  59. /**
  60. * Make a curl call with given options.
  61. * @param array $curl_opts an array of options to curl
  62. * @return array ($info, $header, $response) on success or empty array on error.
  63. */
  64. function do_curl($curl_opts)
  65. {
  66. global $debug;
  67. $retarr = array(); // Return value
  68. if (! $curl_opts) {
  69. if ($debug) { logit("do_curl:ERR:curl_opts is empty"); }
  70. return $retarr;
  71. }
  72. // Open curl session
  73. $ch = curl_init();
  74. if (! $ch) {
  75. if ($debug) { logit("do_curl:ERR:curl_init failed"); }
  76. return $retarr;
  77. }
  78. // Set curl options that were passed in
  79. curl_setopt_array($ch, $curl_opts);
  80. // Ensure that we receive full header
  81. curl_setopt($ch, CURLOPT_HEADER, true);
  82. if ($debug) {
  83. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  84. curl_setopt($ch, CURLOPT_VERBOSE, true);
  85. }
  86. // Send the request and get the response
  87. ob_start();
  88. $response = curl_exec($ch);
  89. $curl_spew = ob_get_contents();
  90. ob_end_clean();
  91. if ($debug && $curl_spew) {
  92. logit("do_curl:INFO:curl_spew begin");
  93. logit($curl_spew, false);
  94. logit("do_curl:INFO:curl_spew end");
  95. }
  96. // Check for errors
  97. if (curl_errno($ch)) {
  98. $errno = curl_errno($ch);
  99. $errmsg = curl_error($ch);
  100. if ($debug) { logit("do_curl:ERR:$errno:$errmsg"); }
  101. curl_close($ch);
  102. unset($ch);
  103. return $retarr;
  104. }
  105. if ($debug) {
  106. logit("do_curl:DBG:header sent begin");
  107. $header_sent = curl_getinfo($ch, CURLINFO_HEADER_OUT);
  108. logit($header_sent, false);
  109. logit("do_curl:DBG:header sent end");
  110. }
  111. // Get information about the transfer
  112. $info = curl_getinfo($ch);
  113. // Parse out header and body
  114. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  115. $header = substr($response, 0, $header_size);
  116. $body = substr($response, $header_size );
  117. // Close curl session
  118. curl_close($ch);
  119. unset($ch);
  120. if ($debug) {
  121. logit("do_curl:DBG:response received begin");
  122. if (!empty($response)) { logit($response, false); }
  123. logit("do_curl:DBG:response received end");
  124. }
  125. // Set return value
  126. array_push($retarr, $info, $header, $body);
  127. return $retarr;
  128. }
  129. /**
  130. * Pretty print some JSON
  131. * @param string $json The packed JSON as a string
  132. * @param bool $html_output true if the output should be escaped
  133. * (for use in HTML)
  134. * @link http://us2.php.net/manual/en/function.json-encode.php#80339
  135. */
  136. function json_pretty_print($json, $html_output=false)
  137. {
  138. $spacer = ' ';
  139. $level = 1;
  140. $indent = 0; // current indentation level
  141. $pretty_json = '';
  142. $in_string = false;
  143. $len = strlen($json);
  144. for ($c = 0; $c < $len; $c++) {
  145. $char = $json[$c];
  146. switch ($char) {
  147. case '{':
  148. case '[':
  149. if (!$in_string) {
  150. $indent += $level;
  151. $pretty_json .= $char . "\n" . str_repeat($spacer, $indent);
  152. } else {
  153. $pretty_json .= $char;
  154. }
  155. break;
  156. case '}':
  157. case ']':
  158. if (!$in_string) {
  159. $indent -= $level;
  160. $pretty_json .= "\n" . str_repeat($spacer, $indent) . $char;
  161. } else {
  162. $pretty_json .= $char;
  163. }
  164. break;
  165. case ',':
  166. if (!$in_string) {
  167. $pretty_json .= ",\n" . str_repeat($spacer, $indent);
  168. } else {
  169. $pretty_json .= $char;
  170. }
  171. break;
  172. case ':':
  173. if (!$in_string) {
  174. $pretty_json .= ": ";
  175. } else {
  176. $pretty_json .= $char;
  177. }
  178. break;
  179. case '"':
  180. if ($c > 0 && $json[$c-1] != '\\') {
  181. $in_string = !$in_string;
  182. }
  183. default:
  184. $pretty_json .= $char;
  185. break;
  186. }
  187. }
  188. return ($html_output) ?
  189. '<pre>' . htmlentities($pretty_json) . '</pre>' :
  190. $pretty_json . "\n";
  191. }
  192. ?>