note 46816 deleted from function.serialize by joey
| From: | joey@php.net | Date: | Sun, 19 Aug 2012 18:18:50 +0000 |
| Subject: | note 46816 deleted from function.serialize by joey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190134@lists.php.net to get a copy of this message | ||
Note Submitter: Alexei dot Filine at cern dot ch
----
A simple way to transfer PHP variables between PHP scripts running on different hosts can be done
with HTTP_Request PEAR module. I think it is much more fast way than one done with XML-RPC but less
portable (there were described ways to exchange vars between Perl and PHP below). Recieved PHP
obects are required to be created before recieving by class definition similar to definition used on
sending side. A script sending variable must output nothing except data sent by function
'sendVariable' so a 'composite variable' is to be used to transfer some
variables by one request.
<?php
// exchange-protocol.php
ini_set( 'include_path', '.' );
require_once "config.php"; // set $PEAR_path
ini_set( 'include_path', ini_get( 'include_path' ).$PEAR_path );
// PEAR module
require_once "HTTP/Request.php";
// magic words for HTTP header
$gzcompressed_serialized_type = 'application/serialized_gzcompressed_PHP_variable';
$serialized_type = 'application/serialized_PHP_variable';
$compression_flag_name = '_compressionFlag_';
// Date in the past
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
// always modified
header( "Last-Modified: ".gmdate( "D, d M Y H:i:s" )." GMT" );
// returns non-zero on success
function recieveVariable( $url,
&$var,
$user = '',
$passw = '',
$compression_flag = 1 ) {
global $gzcompressed_serialized_type;
global $serialized_type;
$req =& new HTTP_Request( "" );
$req->setURL( $url );
$req->setMethod( HTTP_REQUEST_METHOD_POST );
// set user/password if required
if ( $user && $passw ) {
$req->addPostData( $user,
$passw );
}
$response = $req->sendRequest();
$req->clearPostData();
if ( $compression_flag ) {
$req->addPostData( $compression_flag_name, 1 );
}
if ( PEAR::isError( $response ) ) {
echo $response->getMessage();
} else {
$header = $req->getResponseHeader();
if ( $req->getResponseCode() != 200 ||
! ( $header['content-type'] == $gzcompressed_serialized_type ||
$header['content-type'] == $serialized_type ) ) {
// type error message
echo $req->getResponseBody();
} else {
if ( $header['content-type'] == $gzcompressed_serialized_type )
{
$data = gzuncompress( $req->getResponseBody() );
} else {
$data = $req->getResponseBody();
}
$var = unserialize( $data );
return 1;
}
}
return 0;
}
// just for extra flexibility, if data chunk is small enough we can skip gzipping on the sending
side: $compression_flag = 0;
// in other case we use client preference: $compression_flag = $_REQUEST[$compression_flag_name];
function sendVariable( &$var, $compression_flag = 1 ) {
global $gzcompressed_serialized_type;
global $serialized_type;
// Date in the past
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
// always modified
header( "Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
if ( $compression_flag ) {
// set specialised content type to inform about data type
header( 'content-type: '.$gzcompressed_serialized_type );
echo gzcompress( serialize( $var ) );
} else {
// set specialised content type to inform about data type
header( 'content-type: '.$serialized_type );
echo serialize( $var );
}
}
?>
on client side:
<?php
...
require_once "exchange-protocol.php";
...
if (recieveVariable( $url, $var) ) {
// success...
} else {
// there was something wrong...
}
?>
on the server side:
<?php
...
require_once "exchange-protocol.php";
...
// there must not be output before the function is called!!! execution is to 'die' if an
error occures.
sendVariable( $var, $compression_flag )
?>