eZ Publish  [4.0]
ezsoapclient.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // $Id$
00004 //
00005 // Definition of eZSOAPClient class
00006 //
00007 // Bård Farstad <bf@ez.no>
00008 // Created on: <19-Feb-2002 15:42:03 bf>
00009 //
00010 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00011 // SOFTWARE NAME: eZ Publish
00012 // SOFTWARE RELEASE: 4.0.x
00013 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00014 // SOFTWARE LICENSE: GNU General Public License v2.0
00015 // NOTICE: >
00016 //   This program is free software; you can redistribute it and/or
00017 //   modify it under the terms of version 2.0  of the GNU General
00018 //   Public License as published by the Free Software Foundation.
00019 //
00020 //   This program is distributed in the hope that it will be useful,
00021 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 //   GNU General Public License for more details.
00024 //
00025 //   You should have received a copy of version 2.0 of the GNU General
00026 //   Public License along with this program; if not, write to the Free
00027 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00028 //   MA 02110-1301, USA.
00029 //
00030 //
00031 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00032 //
00033 
00034 /*!
00035   \class eZSOAPClient ezsoapclient.php
00036   \ingroup eZSOAP
00037   \brief eZSOAPClient is a class which can be used as a SOAP client
00038 
00039   eZSOAPClient handles communication with a SOAP server.
00040 
00041   \code
00042 
00043 // include client classes
00044 //include_once( "lib/ezsoap/classes/ezsoapclient.php" );
00045 //include_once( "lib/ezsoap/classes/ezsoaprequest.php" );
00046 
00047 // create a new client
00048 $client = new eZSOAPClient( "nextgen.bf.dvh1.ez.no", "/sdk/ezsoap/view/server" );
00049 
00050 $namespace = "http://soapinterop.org/";
00051 
00052 // create the SOAP request object
00053 $request = new eZSOAPRequest( "addNumbers", "http://calkulator.com/simplecalculator" );
00054 
00055 // add parameters to the request
00056 $request->addParameter( "valueA", 42 );
00057 $request->addParameter( "valueB", 17 );
00058 
00059 // send the request to the server and fetch the response
00060 $response = $client->send( $request );
00061 
00062 // check if the server returned a fault, if not print out the result
00063 if ( $response->isFault() )
00064 {
00065     print( "SOAP fault: " . $response->faultCode(). " - " . $response->faultString() . "" );
00066 }
00067 else
00068     print( "Returned SOAP value was: \"" . $response->value() . "\"" );
00069   \endcode
00070 
00071   \sa eZSOAPServer eZSOAPRequest eZSOAPResponse
00072 
00073 */
00074 
00075 //include_once( "lib/ezsoap/classes/ezsoapresponse.php" );
00076 require_once( "lib/ezutils/classes/ezdebug.php" );
00077 
00078 class eZSOAPClient
00079 {
00080     /*!
00081       Creates a new SOAP client.
00082 
00083       \param $server The remote server to connect to
00084       \param $path The path to the SOAP service on the remote server
00085       \param $port The port to connect to, 80 by default. You can use 'ssl' as well to specify that you want to use port 443 over SSL,
00086                    but omit the last parameter $useSSL of this method then or set it to true. When $port equals 443, SSL will also be
00087                    used if $useSSL is omitted or set to true.
00088       \param $useSSL If we need to connect to the remote server with (https://) or without (http://) SSL
00089     */
00090     function eZSOAPClient( $server, $path = '/', $port = 80, $useSSL = null )
00091     {
00092         $this->Login = "";
00093         $this->Password = "";
00094         $this->Server = $server;
00095         $this->Path = $path;
00096         $this->Port = $port;
00097         if ( is_numeric( $port ) )
00098         {
00099             $this->Port = $port;
00100 
00101             if ( $port == 443 )
00102             {
00103                 $this->UseSSL = true;
00104             }
00105         }
00106         elseif ( strtolower( $port ) == 'ssl' )
00107         {
00108             $this->UseSSL = true;
00109             $this->Port = 443;
00110         }
00111         else
00112         {
00113             $this->Port = 80;
00114         }
00115 
00116         if ( $useSSL === true )
00117         {
00118             $this->UseSSL = true;
00119         }
00120         else if ( $useSSL === false )
00121         {
00122             $this->UseSSL = false;
00123         }
00124     }
00125 
00126     /*!
00127       Sends a SOAP message and returns the response object.
00128     */
00129     function send( $request )
00130     {
00131         if ( !$this->UseSSL || !in_array( "curl", get_loaded_extensions() ) )
00132         {
00133             if ( $this->Timeout != 0 )
00134             {
00135                 $fp = fsockopen( $this->Server,
00136                                  $this->Port,
00137                                  $this->errorNumber,
00138                                  $this->errorString,
00139                                  $this->Timeout );
00140             }
00141             else
00142             {
00143                 $fp = fsockopen( $this->Server,
00144                                  $this->Port,
00145                                  $this->errorNumber,
00146                                  $this->errorString );
00147             }
00148 
00149             if ( $fp == 0 )
00150             {
00151                 $this->ErrorString = '<b>Error:</b> eZSOAPClient::send() : Unable to open connection to ' . $this->Server . '.';
00152                 return 0;
00153             }
00154 
00155             $payload = $request->payload();
00156 
00157             $authentification = "";
00158             if ( ( $this->login() != "" ) )
00159             {
00160                 $authentification = "Authorization: Basic " . base64_encode( $this->login() . ":" . $this->password() ) . "\r\n" ;
00161             }
00162 
00163             $HTTPRequest = "POST " . $this->Path . " HTTP/1.0\r\n" .
00164                 "User-Agent: eZ soap client\r\n" .
00165                 "Host: " . $this->Server . ":" . $this->Port . "\r\n" .
00166                 $authentification .
00167                 "Content-Type: text/xml\r\n" .
00168                 "SOAPAction: \"" . $request->namespace() . '/' . $request->name() . "\"\r\n" .
00169                 "Content-Length: " . strlen( $payload ) . "\r\n\r\n" .
00170                 $payload;
00171 
00172             if ( !fputs( $fp, $HTTPRequest, strlen( $HTTPRequest ) ) )
00173             {
00174                 $this->ErrorString = "<b>Error:</b> could not send the SOAP request. Could not write to the socket.";
00175                 $response = 0;
00176                 return $response;
00177             }
00178 
00179             $rawResponse = "";
00180             // fetch the SOAP response
00181             while ( $data = fread( $fp, 32768 ) )
00182             {
00183                 $rawResponse .= $data;
00184             }
00185 
00186             // close the socket
00187             fclose( $fp );
00188         }
00189         else //SOAP With SSL
00190         {
00191             if ( $request instanceof eZSOAPRequest )
00192             {
00193                 $URL = "https://" . $this->Server . ":" . $this->Port . $this->Path;
00194                 $ch = curl_init ( $URL );
00195                 if ( $this->Timeout != 0 )
00196                 {
00197                     curl_setopt( $ch, CURLOPT_TIMEOUT, $this->TimeOut );
00198                 }
00199                 $payload = $request->payload();
00200 
00201                 if ( $ch != 0 )
00202                 {
00203                     $HTTPCall = "POST " . $this->Path . " HTTP/1.0\r\n" .
00204                         "User-Agent: eZ soap client\r\n" .
00205                         "Host: " . $this->Server . ":" . $this->Port . "\r\n" .
00206                         "Content-Type: text/xml\r\n" .
00207                         "SOAPAction: \"" . $request->namespace() . '/' . $request->name() . "\"\r\n" .
00208                         "Content-Length: " . strlen( $payload ) . "\r\n";
00209                     if ( $this->login() != '' )
00210                     {
00211                         $HTTPCall .= "Authorization: Basic " .  base64_encode( $this->login() . ":" . $this->Password() ) . "\r\n";
00212                     }
00213                     $HTTPCall .= "\r\n" . $payload;
00214 
00215                     curl_setopt( $ch, CURLOPT_URL, $URL );
00216                     curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
00217                     curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 1 );
00218                     curl_setopt( $ch, CURLOPT_HEADER, 1 );
00219                     curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
00220                     curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $HTTPCall );  // Don't use CURLOPT_CUSTOMREQUEST without making sure your server supports the custom request method first.
00221                     unset( $rawResponse );
00222 
00223                     if ( $ch != 0 )
00224                     {
00225                         $rawResponse = curl_exec( $ch );
00226                     }
00227                     if ( !$rawResponse )
00228                     {
00229                         $this->ErrorString = "<b>Error:</b> could not send the XML-SOAP with SSL call. Could not write to the socket.";
00230                         $response = 0;
00231                         return $response;
00232                     }
00233                 }
00234 
00235                 curl_close( $ch );
00236             }
00237         }
00238         $response = new eZSOAPResponse();
00239         $response->decodeStream( $request, $rawResponse );
00240         return $response;
00241     }
00242 
00243     /*!
00244      Set timeout value
00245 
00246      \param timeout value in seconds. Set to 0 for unlimited.
00247     */
00248     function setTimeout( $timeout )
00249     {
00250         $this->Timeout = $timeout;
00251     }
00252 
00253     /*!
00254      Sets the HTTP login
00255     */
00256     function setLogin( $login  )
00257     {
00258         $this->Login = $login;
00259     }
00260 
00261     /*!
00262       Returns the login, used for HTTP authentification
00263     */
00264     function login()
00265     {
00266         return $this->Login;
00267     }
00268 
00269     /*!
00270      Sets the HTTP password
00271     */
00272     function setPassword( $password  )
00273     {
00274         $this->Password = $password;
00275     }
00276 
00277     /*!
00278       Returns the password, used for HTTP authentification
00279     */
00280     function password()
00281     {
00282         return $this->Password;
00283     }
00284 
00285     /// The name or IP of the server to communicate with
00286     public $Server;
00287     /// The path to the SOAP server
00288     public $Path;
00289     /// The port of the server to communicate with.
00290     public $Port;
00291     /// How long to wait for the call.
00292     public $Timeout = 0;
00293     /// HTTP login for HTTP authentification
00294     public $Login;
00295     /// HTTP password for HTTP authentification
00296     public $Password;
00297     private $UseSSL;
00298 }
00299 
00300 ?>