PHP Classes
elePHPant
Icontem

PHP JSONP Response: Encode and generate a response to JSONP request

Recommend this page to a friend!
  Info   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2014-10-02 (1 year ago) RSS 2.0 feedNot enough user ratingsTotal: 539 This week: 1All time: 5,284 This week: 963Up
Version License PHP version Categories
jsonp 1.0GNU General Publi...4.0PHP 5, Web services, AJAX
Description Author

This class can encode and generate a response to JSONP request.

It generates JavaScript code to return JSON encoded variable value as response to a JSONP request.

The generated JavaScript may either invoke a callback function with a name defined by a request variable, or assign a JavaScript variable with the JSON encoded variable value.

Innovation Award
PHP Programming Innovation award nominee
May 2013
Number 3


Prize: One downloadable e-book of choice by O'Reilly
JSONP is a form to respond to AJAX requests by generating JavaScript that will call a given callback function.

This class can facilitate the generation of JSONP request responses from PHP variable values.

Manuel Lemos
Picture of Radovan Janjic
  Performance   Level  
Name: Radovan Janjic is available for providing paid consulting. Contact Radovan Janjic .
Classes: 14 packages by
Country: Serbia Serbia
Age: 28
All time rank: 3172 in Serbia Serbia
Week rank: 42 Up2 in Serbia Serbia Equal
Innovation award
Innovation award
Nominee: 3x

Details

PHP_JSONP_Response

Encode and generate a response to JSONP request

This class can encode and generate a response to JSONP request.

It generates JavaScript code to return JSON encoded variable value as response to a JSONP request.

The generated JavaScript may either invoke a callback function with a name defined by a request variable, or assign a JavaScript variable with the JSON encoded variable value.

Example

PHP

require 'JSONP.class.php';

// Print output with correct headers informations
JSONP::output(array('foo' => 'bar'));

jQuery call

$.ajax({
	// Call to url
	url: '/path/to/example.php',
	
	// Data Type
	dataType: 'jsonp',
	
	// Send data
	data: {
	  foo: 'bar',
	  baz: 'qux'
	},
	
	// On success call
	success: function(data) {
		// Do stuff with data object 
		if (data.foo == 'bar') {
			alert('Baz!');
		}
	}
});

Other PHP examples

require 'JSONP.class.php';

// JSON encode data
$json_encoded = JSONP::encode(array('foo', 'bar'));

// JSONP string as returned value
$jsonp_encoded = JSONP::output(array('foo', 'bar'), FALSE);

// Print output
JSONP::output(array('foo', 'bar'));

// Print output without headers
JSONP::output(array('foo', 'bar'), TRUE, FALSE);
  Files folder image Files  
File Role Description
Accessible without login Plain text file captcha.php Example Example script
Accessible without login Plain text file example.html Example Example script
Plain text file JSONP.class.php Class Class file
Accessible without login Plain text file JSONP.class.php4 Data Auxiliary data
Plain text file JSONP.js Class JavaScript package file
Accessible without login Plain text file README.md Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:539
This week:1
All time:5,284
This week:963Up