PHP Classes

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
2024-01-09 (3 days ago) RSS 2.0 feedNot enough user ratingsTotal: 592 This week: 3All time: 5,231 This week: 141Up
Version License PHP version Categories
jsonp 1.9GNU 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 <contact>
Classes: 14 packages by
Country: Serbia Serbia
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:592
This week:3
All time:5,231
This week:141Up