PageRenderTime 25ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/simple/index.php

https://github.com/ddumas/php-simple-proxy
PHP | 249 lines | 171 code | 45 blank | 33 comment | 4 complexity | 22ddd0ffd1af3080e79a4dc525613bed MD5 | raw file
  1. <?PHP
  2. include "../index.php";
  3. $shell['title3'] = "It's just that simple";
  4. $shell['h2'] = 'Get external HTML, JSON and more!';
  5. // ========================================================================== //
  6. // SCRIPT
  7. // ========================================================================== //
  8. ob_start();
  9. ?>
  10. $(function(){
  11. // Handle form submit.
  12. $('#params').submit(function(){
  13. var proxy = '../../ba-simple-proxy.php',
  14. url = proxy + '?' + $('#params').serialize();
  15. // Update some stuff.
  16. $('#request').html( $('<a/>').attr( 'href', url ).text( url ) );
  17. $('#response').html( 'Loading...' );
  18. // Test to see if HTML mode.
  19. if ( /mode=native/.test( url ) ) {
  20. // Make GET request.
  21. $.get( url, function(data){
  22. $('#response')
  23. .html( '<pre class="brush:xml"/>' )
  24. .find( 'pre' )
  25. .text( data );
  26. SyntaxHighlighter.highlight();
  27. });
  28. } else {
  29. // Make JSON request.
  30. $.getJSON( url, function(data){
  31. $('#response')
  32. .html( '<pre class="brush:js"/>' )
  33. .find( 'pre' )
  34. .text( JSON.stringify( data, null, 2 ) );
  35. SyntaxHighlighter.highlight();
  36. });
  37. }
  38. // Prevent default form submit action.
  39. return false;
  40. });
  41. // Submit the form on page load if ?url= is passed into the example page.
  42. if ( $('#url').val() !== '' ) {
  43. $('#params').submit();
  44. }
  45. // Disable AJAX caching.
  46. $.ajaxSetup({ cache: false });
  47. // Disable dependent checkboxes as necessary.
  48. $('input:radio').click(function(){
  49. var that = $(this),
  50. c1 = 'dependent-' + that.attr('name'),
  51. c2 = c1 + '-' + that.val();
  52. that.closest('form')
  53. .find( '.' + c1 + ' input' )
  54. .attr( 'disabled', 'disabled' )
  55. .end()
  56. .find( '.' + c2 + ' input' )
  57. .removeAttr( 'disabled' );
  58. });
  59. // Clicking sample remote urls should populate the "Remote URL" box.
  60. $('#sample a').click(function(){
  61. $('#url').val( $(this).attr( 'href' ) );
  62. return false;
  63. });
  64. });
  65. <?
  66. $shell['script'] = ob_get_contents();
  67. ob_end_clean();
  68. // ========================================================================== //
  69. // HTML HEAD ADDITIONAL
  70. // ========================================================================== //
  71. ob_start();
  72. ?>
  73. <script type="text/javascript" language="javascript">
  74. // I want to use json2.js because it allows me to format stringified JSON with
  75. // pretty indents, so let's nuke any existing browser-specific JSON parser.
  76. window.JSON = null;
  77. </script>
  78. <script type="text/javascript" src="../../shared/json2.js"></script>
  79. <script type="text/javascript" language="javascript">
  80. <?= $shell['script']; ?>
  81. $(function(){
  82. // Syntax highlighter.
  83. SyntaxHighlighter.defaults['auto-links'] = false;
  84. SyntaxHighlighter.highlight();
  85. });
  86. </script>
  87. <style type="text/css" title="text/css">
  88. /*
  89. bg: #FDEBDC
  90. bg1: #FFD6AF
  91. bg2: #FFAB59
  92. orange: #FF7F00
  93. brown: #913D00
  94. lt. brown: #C4884F
  95. */
  96. #page {
  97. width: 700px;
  98. }
  99. #params input.text {
  100. display: block;
  101. border: 1px solid #000;
  102. width: 540px;
  103. padding: 2px;
  104. margin-bottom: 0.2em;
  105. }
  106. #params input.submit {
  107. display: block;
  108. margin-top: 0.6em;
  109. }
  110. .indent {
  111. margin-left: 1em;
  112. }
  113. #sample {
  114. font-size: 90%;
  115. }
  116. </style>
  117. <?
  118. $shell['html_head'] = ob_get_contents();
  119. ob_end_clean();
  120. // ========================================================================== //
  121. // HTML BODY
  122. // ========================================================================== //
  123. ob_start();
  124. ?>
  125. <?= $shell['donate'] ?>
  126. <p>
  127. With <a href="http://benalman.com/projects/php-simple-proxy/">Simple PHP Proxy</a>, your JavaScript can
  128. access content in remote webpages, without cross-domain security limitations, even if it's not available
  129. in JSONP format. Of course, you'll need to install this PHP script on your server.. but that's a small
  130. price to have to pay for this much awesomeness.
  131. </p>
  132. <p>
  133. Please note that while jQuery is used here, you can use any library you'd like.. or just code your
  134. XMLHttpRequest objects by hand, it doesn't matter. This proxy just acts a bridge between the client
  135. and server to facilitate cross-domain communication, so the client-side JavaScript is entirely left
  136. up to you (but I recommend jQuery's <a href="http://docs.jquery.com/Ajax/jQuery.getJSON">getJSON</a>
  137. method because of its simplicity).
  138. </p>
  139. <p>
  140. Please see the <a href="http://benalman.com/projects/php-simple-proxy/">project page</a> and
  141. <a href="http://benalman.com/code/projects/php-simple-proxy/docs/">documentation</a> for more usage
  142. information.
  143. </p>
  144. <form id="params" method="get" action="">
  145. <div>
  146. <label>
  147. <b>Remote URL</b>
  148. <input id="url" class="text" type="text" name="url" value="<?= $_GET['url'] ?>">
  149. </label>
  150. </div>
  151. <p id="sample">
  152. ..or try these sample Remote URLs:
  153. <a href="http://github.com/">GitHub</a>,
  154. <a href="http://github.com/cowboy/php-simple-proxy/raw/master/examples/simple/json_sample.js">a sample JSON (not JSONP) request</a>,
  155. <a href="http://github.com/omg404errorpage">a 404 error page</a>
  156. </p>
  157. <div>
  158. <label>
  159. <input type="radio" name="mode" value="native" disabled="disabled">
  160. Native <i>(disabled by default)</i>
  161. </label>
  162. </div>
  163. <div>
  164. <label>
  165. <input type="radio" name="mode" value="json" checked="checked" disabled="disabled">
  166. JSON
  167. </label>
  168. </div>
  169. <div class="dependent-mode dependent-mode-json indent">
  170. <div>
  171. <label>
  172. <input type="checkbox" name="full_headers" value="1" checked="checked">
  173. Full Headers
  174. </label>
  175. </div>
  176. <div>
  177. <label>
  178. <input type="checkbox" name="full_status" value="1" checked="checked">
  179. Full Status
  180. </label>
  181. </div>
  182. </div>
  183. <input class="submit" type="submit" name="submit" value="Submit">
  184. </form>
  185. <h3>Request URL</h3>
  186. <p id="request">N/A, click Submit!</p>
  187. <h3>Simple PHP Proxy response</h3>
  188. <div id="response">N/A, click Submit!</div>
  189. <h3>The code</h3>
  190. <pre class="brush:js">
  191. <?= htmlspecialchars( $shell['script'] ); ?>
  192. </pre>
  193. <?
  194. $shell['html_body'] = ob_get_contents();
  195. ob_end_clean();
  196. // ========================================================================== //
  197. // DRAW SHELL
  198. // ========================================================================== //
  199. draw_shell();
  200. ?>