Skip to content

Instantly share code, notes, and snippets.

@jamesmontalvo3
Last active August 29, 2015 14:17
Show Gist options
  • Save jamesmontalvo3/47b44bfe2b8d46cc22a1 to your computer and use it in GitHub Desktop.
Save jamesmontalvo3/47b44bfe2b8d46cc22a1 to your computer and use it in GitHub Desktop.
PHP implementation for returning JSON and HTML via JSONP
<?php
// set content type to javascript (else it'll think it's HTML)
header( 'Content-Type: application/javascript; charset=utf-8' );
// allow the requester to specify a callback function
$callback = isset( $_GET['callback'] ) ? $_GET['callback'] : 'callback';
// retrieve the contents of the current event's HTML file
$configHtmlText = file_get_contents( __DIR__ . '/Event1069.html' );
// stick the html string into a php array (with key "html") then
// create JSON out of it
$stringifiedJson = json_encode( array(
'html' => $configHtmlText
) );
// return javascript callback function
echo "$callback( $stringifiedJson );";
<?php
// set content type to javascript (else it'll think it's HTML)
header( 'Content-Type: application/javascript; charset=utf-8' );
// allow the requester to specify a callback function
$callback = isset( $_GET['callback'] ) ? $_GET['callback'] : 'callback';
// retrieve the contents of the current event's HTML file
$configJsonText = file_get_contents( __DIR__ . '/Event1069.json' );
// return javascript callback function
echo "$callback( $configJsonText );";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment