note 46816 added to function.serialize

From: Date: Sun, 24 Oct 2004 14:57:53 +0000
Subject: note 46816 added to function.serialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-79032@lists.php.net to get a copy of this message
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 ) ?> ---- Manual Page -- http://www.php.net/manual/en/function.serialize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46816 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46816&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46816&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46816&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46816&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46816&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46816&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46816&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#79032) next »