PHP Classes

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

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (9 months ago) RSS 2.0 feedStarStarStar 44%Total: 595 All time: 5,231 This week: 37Up
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 is available for providing paid consulting. Contact Radovan Janjic .
Classes: 14 packages by
Country: Serbia Serbia
Age: 36
All time rank: 2382 in Serbia Serbia
Week rank: 50 Up1 in Serbia Serbia Up
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 (6)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:595
This week:0
All time:5,231
This week:37Up
 User Ratings  
 
 All time
Utility:66%StarStarStarStar
Consistency:66%StarStarStarStar
Documentation:-
Examples:50%StarStarStar
Tests:-
Videos:-
Overall:44%StarStarStar
Rank:3441