EZ Question: Can PHP & Cron do this?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
zander213
Forum Newbie
Posts: 13
Joined: Mon Aug 23, 2004 1:46 pm

EZ Question: Can PHP & Cron do this?

Post by zander213 »

I currently have 3 websites...
I would like to link these 3 sites to 3 datafeeds that are on an FTP site...
The datafeeds are for unrelated products, but they're zipped into a single .zip file (on the FTP site)...

Here is my question:

If I set up a 4th server to download the .zip file will I be able to divide up the datafeeds and send them to their respective sites using PHP and Cron? If so, how do I explain this to whoever is going to do the programming? What are the functions called that I will be asking for?

Any help would be greatly appreciated...

Bob
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Yes to FTP.

Here is a stab I took at a mini-FTP client when I needed it at work (older stuff PHP4) I would show you the tests, but they were from early in my testing experience and look pretty pathetic ;).

Code: Select all

class FtpAgent
{
    /**
     *    @var    string    host name
     */
    var $_asHost=false;
    /**
     *    @var    string    user name
     */
    var $_asUser=false;
    /**
     *    @var    string    user password
     */
    var $_asPass='';
    
    /**
     *    constructor
     *    @param    string    $psHost        optional - host name
     *    @param    string    $psUser        optional - user name
     *    @param    string    $psPw        optional - user password
     *    @return    void
     */
    function FtpAgent($psHost=false, $psUser=false, $psPw=false)
    {
        if ($psHost) {
            $this->SetHost($psHost);
        }
        if ($psUser) {
            $this->SetUser($psUser, $psPw);
        }
    }

    /**
     *    validate host name
     *    @param    string    $psHost        host name
     *    @return    boolean
     */
    function SetHost($psHost)
    {
        if ($psHost == gethostbyname($psHost)) {
            trigger_error("Unable to locate host '$psHost'");
            return false;
        }
        $this->_asHost = $psHost;
        return true;
    }
    
    /**
     *    set user name and password
     *    @param    string    $psUser        user name
     *    @param    string    $psPw        user password
     *    @return    boolean
     */
    function SetUser($psUser, $psPw='')
    {
        if (is_string($psUser)
            && preg_match('/^[a-z]+\w*$/i', trim($psUser))) {
            if (preg_match('/^[a-z]?\w*$/i', trim($psPw))) {
                $this->_asUser = trim($psUser);
                $this->_asPass = trim($psPw);
                return true;
            } else {
                trigger_error("Invalid Password '$psPw'");
                return false;
            }
        } else {
            trigger_error("Invalid User Name '$psUser'");
            return false;
        }
            
    }
    
    /**
     *
     *    @return    boolean
     */
    function GetFile($psFile, $psLocal, $pbBin=false)
    {
        //do some input validation here
        $b_ret = true;
        if ($this->_Connect($this->_asHost)) {
            if ($this->_Login()) {
                $this->_Fetch($psFile, $psLocal, (boolean)$pbBin);
            } else {
                trigger_error("Unable to login to '{$this->_asHost}' as '{$this->_asUser}'");
                $b_ret = false;
            }
            $b_ret = ($b_ret && $this->_Close());
            return $b_ret;
        } else {
            return false;
        }
    }
    
    /**
     *
     *    @return
     */
    function PutFile()
    {
    }

    /**
     *
     *    @return    boolean
     */
    function _Connect()
    {
        if (is_string($this->_asHost)
            && strlen($this->_asHost) > 0) {
            $r_ftp = ftp_connect($this->_asHost);
            if (is_resource($r_ftp)) {
                $this->_arFtp = $r_ftp;
                return true;
            } else {
                trigger_error('Error In establishing FTP Connection');
                return false;
            }
        }
        trigger_error('Host Not Set, use FtpAgent::SetHost first');
        return false;
    }
    
    /**
     *
     *    @return    boolean
     */
    function _Login()
    {
        if (is_resource($this->_arFtp)) {
            return ftp_login($this->_arFtp, $this->_asUser, $this->_asPass);
        }
        return false;
    }
    
    /**
     *
     *    @return
     */
    function _Fetch($psFile, $psLocal, $pbBin)
    {
        if (is_resource($this->_arFtp)) {
            $i_mode = ($pbBin) ? FTP_BINARY : FTP_ASCII;
            return ftp_get($this->_arFtp, $psLocal, $psFile, $i_mode);
        }
        return false;
    }
    
    /**
     *
     *    @return    boolean
     */
    function _Close()
    {
        if (is_resource($this->_arFtp)) {
            ftp_close($this->_arFtp);
            return true;
        }
        return false;
    }
}
I have never had the need to deal with zip files inside of PHP, but this looks more than up to the job.

Lastly, for the overall script I would probably have the PHP script
1) download the zip file
2) extract to a file
3) loop over the file and extract into three separate data files
4) do whatever you need to do to get to the other servers, i.e. re-ftp, copy to a different directory, etc.
5) notify the other servers they need to process the data.

HTH
zander213
Forum Newbie
Posts: 13
Joined: Mon Aug 23, 2004 1:46 pm

Post by zander213 »

Thanks sweatje... that's all I needed to know. I appreciate your help!

Cheers!

Bob
Post Reply