Sunday, November 29, 2009

Simple Header Reader

Well I tried doing a search on this myself but after many unsuccessful searches I decided to tackle the task myself (I really thought I would find something quickly). I figured it would be simple so someone must have done something on the topic before and I proved myself wrong - well about the finding part, it was rather simple.

Anyway what this little function does is read only the headers of a page using PhP's curl() functions and returns them as an associative array. A great way to check if you need to update an RSS cache or something similar.

function checkHeaders($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$content = curl_exec($ch);
curl_close($ch);
preg_match_all('/[^:]*[\r\n]|([^:]*):(.*)[\r\n]/', $content, $matches, PREG_SET_ORDER);
foreach ( $matches as $match ) {
if ( !empty($match[0]) ) {
if( strstr($match[0], 'HTTP') === false )
$headers[$match[1]] = $match[2];
else
$headers['HTTP'] = $match[0];
}
}
return $headers;
}

The HTTP header index will simply be 'HTTP' every other header will be in the form <header> => <value>.
example:

['Content-Type'] => ['text/xml']

enjoy :)

Tuesday, November 24, 2009

PHP Curl download via FTP access - Part 1

Downloading files via FTP in PhP is fairly easy even if you need FTP access. If your host supports it you just grab the file via PhP's fgets() function (or fread() if u need binary-safe). If you do need FTP access heres a quick snippet to connect:

$fp = ftp_connect($ftpServer);
$lh = ftp_login($fp, $ftpUser, $ftpPass);
$data = fgets($fp);
And you have your file;

additionally you can add a file_put_contents($local, $data); to write it to your local server.
However if your host doesn't support this you'll have to go with the curl method. First step is to make sure the library is installed on your server. Most come with it pre-installed these days so you should be good but to check simple echo phpinfo() and search for "curl" to see what you have.
Next step is to login to FTP via curl which you can do using this format:
$url = "ftp://$ftpUser:$ftpPass@$ftpServer";
it is a good idea to urlencode() your $ftpUser and $ftpPass because if it has a semicolon or at symbol most servers won't know how to interpret what you are trying to do. (Took me like an hour to figure out why it wasn't working for me :\). Once you know you have access you can continue the script and download a remote file to your local server as follows:

$ftpServer = <hosted_file>;
$ftpUser = urlencode(<ftp_user_name>);
$ftpPass = urlencode(<ftp_password>);
$local = <local_file_location>; //be sure to use full path here
$url = "ftp://$ftpUser:$ftpPass@$ftpServer";
$cp = curl_init();
$fp = fopen($local, "w");
curl_setopt($cp, CURLOPT_URL,$url);
curl_setopt($cp, CURLOPT_FILE, $fp);
curl_setopt($cp, CURLOPT_HEADER, 0);

curl_exec($cp);
curl_close($cp);
fclose($fp);

Hope this saves someone hours of time researching.

Tuesday, November 3, 2009

Setting Sitewide URL Prefix in Zend

So recently I came across the need to put a site into a test folder. Now I have done this in the past and it is very easy using a global domain name variable to change your domain from http://something.com/ to http://something.com/test/. If you have you own routing class you can just tell it to ignore the first parameter in the URI.

But here is the problem: I was trying to do this with the Zend routing class on a site that was already configured at setup with all its routing rules. Originally I tried just prepending 'test/' to all of the existing rules but that caused more complications than I had time to fix. I tried Googling a quick fix for this but could find nothing other than how to reroute one rule to another or forward to another controller. While it was useful information and I may need it in the future it was unrelated to my issue.

What I ended up doing was digging through the Zend_Controller_Front class to see what I could find. And voila I came across a class variable _baseUrl. Looking through the class methods I quickly found the setBaseUrl() method. After smacking myself on the head for not thinking of this earlier I then set called the already set controller class to call
Zend_Controller_Front::setBaseUrl('/test/');
And like magic the Zend controller started recognizing all my URL's that were prepended with '/test/'.

Hope this saves someone else a couple hours of searching.