RSS Feed

Peter Goodman's blog about PHP, Parsing Theory, Javascript, C++, Applications,

Working with PHP Sockets and cURL

I was recently asked to release OneLobby's source code. It's an unfinished product but I think it would be a good idea. However, before I do that I'm going to showcase some of the useful classes I made for it. Four of these classes happen to be interfaces for dealing with PHP's socket and cURL functions. Take a look...

<?php

/*
OneLobby
Copyright (C) 2007 Peter Goodman, Geoffrey Goodman

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

//-------------------------------------------------
// The user Agent and version of this OneLobby remote
// XML caller.
//-------------------------------------------------

define ('REMOTE_XML_USERAGENT''OneLobby [RemoteXML v.1.0]');

//-------------------------------------------------
// Out little extension class.. it's not really needed,
// but it's convenient to choose REST or RPC.
//-------------------------------------------------

class RemoteXML extends FAExtension
{
    function 
RemoteXML()
    {
        
// moo :P
    
}
    
    function &
getFinder($type 'REST')
    {
        
$class $type .'_Request';
        
$ret NULL;
        
        if(
class_exists($class))
        {
            
$ret = &new $class();
        }
        
        return 
$ret;
    }
}

//-------------------------------------------------
// Base wrapper class for REST and RPC calls. Deal with
// opening connections using sockets or cURL, sending
// and receiving data, etc.
//-------------------------------------------------

class RemoteXMLRequest
{
    
//-------------------------------------------------
    // Some variables related to RPC and REST requests.
    //-------------------------------------------------
    
    
var $_initial_request FALSE;
    var 
$_initial_put FALSE;
    
    
//-------------------------------------------------
    // Get a cURL or Socket connection.
    //-------------------------------------------------
    
    
function &getConnection()
    {
        
//-------------------------------------------------
        // If the cURL extension isn't loaded, try to load
        // it. The PHP_SHLIB_SUFFIX is for checking if we are
        // on Windows or UNIX.
        //-------------------------------------------------

        
if(!extension_loaded('curl'))
        {
            
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' '';
            @
dl($prefix 'curl.'PHP_SHLIB_SUFFIX);
        }
        
        if(
function_exists('curl_init'))
        {
            
$conn = &new cURL_Connection;
        }
        else
        {
            
$conn = &new Socket_Connection;
        }
        
        return 
$conn;
    }
    
    
//-------------------------------------------------
    // Interface methods...
    //-------------------------------------------------
    
    
function getInfo(http://www.ioreader.com)
    {
        
assert(FALSE);
    }
    
    function 
putInfo(http://www.ioreader.com$xml)
    {
        
assert(FALSE);
    }
}

//-------------------------------------------------
// Deal with REST (representational state transfers)
// requests.
//-------------------------------------------------

class REST_Request extends RemoteXMLRequest
{
    
//-------------------------------------------------
    // Do the initial request of data for this REST session.
    //-------------------------------------------------
    
    
function getInfo(http://www.ioreader.com)
    {    
        
//-------------------------------------------------
        // Get out socket/cURL connection and make the request.
        //-------------------------------------------------
        
        
$conn = &$this->getConnection();
        
$conn->createRequest('GET'http://www.ioreader.com);
        
$data $conn->getReturnData();
        
$conn->closeRequest();
        
        
$this->_initial_request TRUE;
        
        return 
$data;
    }
    
    
//-------------------------------------------------
    // Put info to the server.
    //-------------------------------------------------
    
    
function putInfo(http://www.ioreader.com$xml)
    {
        
//-------------------------------------------------
        // Make sure we're doing things the REST way.
        //-------------------------------------------------
        
        
if(!$this->_initial_request)
        {
            
trigger_error("[ERROR] This is a REST application, not RPC.");
        }
        
        
//-------------------------------------------------
        // Use a socket connection for this for the sake of
        // simplicity.
        //-------------------------------------------------
        
        
$conn = &new Socket_Connection;
        
        
$conn->createRequest('PUT'http://www.ioreader.com);
        
$conn->setPutData($xml);
        
$data $conn->getReturnData();
        
$conn->closeRequest();
        
        return 
$data;
    }
}

//-------------------------------------------------
// Deal with RPC (remote procedure call)
// requests.
//-------------------------------------------------

class RPC_Request extends RemoteXMLRequest
{    
    
//-------------------------------------------------
    // Put info to the server.
    //-------------------------------------------------
    
    
function putInfo(http://www.ioreader.com$xml)
    {    
        
//-------------------------------------------------
        // Use a socket connection for this for the sake of
        // simplicity.
        //-------------------------------------------------
        
        
$conn = &new Socket_Connection;
        
        
$conn->createRequest('PUT'http://www.ioreader.com);
        
$conn->setPutData($xml);
        
$data $conn->getReturnData();
        
$conn->closeRequest();
        
        
$this->_initial_put TRUE;
        
        return 
$data;
    }
    
    
//-------------------------------------------------
    // Get the information after out inital PUT
    //-------------------------------------------------
    
    
function getInfo(http://www.ioreader.com)
    {    
        
//-------------------------------------------------
        // Make sure we're doing things the RPC way.
        //-------------------------------------------------
        
        
if(!$this->_initial_put)
        {
            
trigger_error("[ERROR] This is a REST application, not RPC."E_USER_ERROR);
        }
        
        
//-------------------------------------------------
        // Get out socket/cURL connection and make the request.
        //-------------------------------------------------
        
        
$conn = &$this->getConnection();
        
$conn->createRequest('GET'http://www.ioreader.com);
        
$data $conn->getReturnData();
        
$conn->closeRequest();
        
        return 
$data;
    }
}

//-------------------------------------------------
// Interface for cURL and socket connections.
//-------------------------------------------------

class RemoteXMLConnection
{
    var 
$_conn;
    var 
$_conn_closed FALSE;
    
    
//-------------------------------------------------
    // Some nice variables
    //-------------------------------------------------
    
    
var $_scheme;
    var 
$_server;
    var 
$_path;
    var 
$_port 80;
    var 
$_timeout 5;
    
    var 
$_put FALSE;
    var 
$_head FALSE;
    
    function 
createRequest($type 

Comments


Comment