Date Parsing with ColdFusion / Twitter June 2, 2009
Posted by scoopseven in ColdFusion.Tags: Twitter
2 comments
The Twitter API is great. Except for the dates it returns, especially if you’re a ColdFusion progammer. I ended up using some code posted by Peter Freitag to write a UDF to convert the Twitter date/time to a ColdFusion friendly date/time.
Twitter makes matters worse with it’s inconsistency between the REST and Search APIs. My UDF also deals with the different date formats returned by the Twitter APIs.
Search API returns: Sat, 16 May 2009 05:26:32 +0000
REST API returns: Sat May 16 05:26:32 +0000 2009
Nice.
*As of May 2009 the Twitter REST API and Search API still return different date formats. http://code.google.com/p/twitter-api/issues/detail?id=206
If you’re interested in the udf, comment or email me at mark at mediapost.com. Unless it’s a weekend I’ll get it to you within a couple of hours.
Get the First x Words from a String / Word Count in ColdFusion April 30, 2009
Posted by scoopseven in ColdFusion.add a comment
Get the first X words in a string:
#REReplace(str,”^(#RepeatString(‘[^ ]* ‘,maxwords)#).*”,“\1″)#
Rough word count, which will miss hyphen-ated or other/words.
#ArrayLen(commentContent.split(‘\s++’))#
JDBC Connection Strings for Coldfusion 6.1 / CFMX April 14, 2009
Posted by scoopseven in ColdFusion, Database, MySQL.add a comment
If you’re trying to setup ColdFusion MX 6 with MySQL here’s how you setup the MySQL datasources. In CF Administrator go to Datasources and Add a New datasource of type “Other”. Enter your datasource name, username and password and the following fields like this:
JDBC URL: jdbc:mysql://192.168.1.100/DatabaseName
Driver Class: com.mysql.jdbc.Driver
Driver Name:MySQL Connector/J
Make sure you have the latest MySQL drivers installed too.
Getting the link from A tags /Getting all the links from a string in ColdFusion April 2, 2009
Posted by scoopseven in ColdFusion, MediaPost.add a comment
I thought for sure that this would be something fairly easy to find. Alas, it was not.
I did find a way to get all of the text from between the a tag, but not the links. Here’s how you do it:
<cfset beginlink = REFindNoCase(“https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?”, myText)>
<cfset endlink = findnocase(‘”‘, myText, beginlink)>
<cfset linklen = endlink – beginlink>
<cfset mylink = mid(myText, beginlink, linklen)>
Obviously, you can optimize this into a couple of lines, but there it is.
Virtual Hosts with Jboss / Apache / ColdFusion March 25, 2009
Posted by scoopseven in ColdFusion, Linux, MediaPost.add a comment
ServerName www.mysite.comServerAlias sitetwo #added this line to Apache .conf file.
<jboss-web><context-root> /</context-root><virtual-host>www.mysite.com</virtual-host><virtual-host>sitetwo.mysite.com</virtual-host></jboss-web>
Spidering and Saving Images with CFHTTP and CFFILE March 11, 2009
Posted by scoopseven in ColdFusion, Linux, MediaPost.add a comment
Goal: To spider a site (our own site) and save all image files from the pages on that site to a hard disk on another server.
Thanks to Ben Nadel for showing me the way on this one. For some reason the path/file attributes on cfhttp were not allowing me to save a image file to the hard disk on our linux (CentOS) server. I thought I could save the file using cfcontent somehow, which led me to Ben’s post. Never thought to just save the binary stream using cffile. Not sure if there was a permissions problem or what, but here’s the work-around.
<!— Grab the image file. —>
<cfhttp
method=”get”
url=”http://www.myurl.com/someimage.jpg”
useragent=”#CGI.http_user_agent#”
getasbinary=”yes”
result=”objGet” /><!— Save the image file. —>
<cffile action=”write”
output=”#objGet.FileContent#”
mode=”777″
file=”/mypath/myimage.jpg”>
This worked like a charm. Again, not sure if this was cfhttp doesn’t support the “mode” attribute or what, but it helped immensely. Thanks, Ben.
FTP from server to server using ColdFusion June 30, 2008
Posted by scoopseven in ColdFusion, Linux, Windows.add a comment
Here’s a brief set of instructions on how to ftp a file (or many files) from one server/computer to another using ColdFusion.
On Windows:
Download and install NcFtp Client from Microsoft Windows at: http://www.ncftp.com/download/
Make sure you’re NcFtp directory is in your PATH statement so you can run it from anywhere.
Test it by running something like this from a DOS prompt (where myHost is the IP address or host for your ftp server:
ncftpput -P -u myUser -p myPass myHost myDestinationFile.txt C:\myLocalFile.txt
Once you get NcFTP working, you want to create a .bat file and put your successful FTP command in it, let’s call it autoFtp.bat. Now we’re going to call autoFtp.bay from CF:
<cfset argumentsArray = arrayNew(1)>
<cfexecute name = “c:\autoFtp.bat”
arguments = “#argumentsArray#”
timeout = “6000″>
</cfexecute>
Don’t worry about the argumentsArray, we’ll get to that in a bit. Put the above code in a .cfm file and run it. Your file should successfully ftp to the remote server. When you can do that, you can start passing in arguments to your autoFtp.bat file. Edit autoFtp.bat to look like this:
ncftpput -P -u myUser -p myPass myHost %1 %2
Edit your .cfm page so it looks like this:
<cfset argumentsArray = arrayNew(1)
<cfset argumentsArray[1] = “myDestinationFile.txt”>
<cfset argumentsArray[2] = “C:\myLocalFile.txt”>
<cfexecute name = “c:\autoFtp.bat”
arguments = “#argumentsArray#”
timeout = “6000″>
</cfexecute>
When you pass argumentsArray to autoFtp.bat, you can reference them by their array index, so argumentsArray[1] will be referenced as %1 in autoFtp.bat.
On Linux:
A little bit easier, no need fro NcFTP. Instead, create a shell script, .sh file called autoFtp.sh. The contents are below:
#! /bin/sh
DESTINATION_FILE=$1
LOCAL_FILE=$2
HOST=’ftp.myhost.com’
USER=’myUserName’
PASSWD=’myPassword’
ftp -nv <<EOF
open $HOST
user $USER $PASSWD
binary
put $LOCAL_FILE $DESTINATION_FILE
EOF
Make sure you have execute permissions on the autoFtp.sh. I’m using this to transfer images, so the “binary” command is totally necessary for me. The arguments are passed into autoFtp.sh on the command line, the first argument is referenced by $1, the second, $2, etc. Good Luck!
Alternatives to cflocation March 12, 2008
Posted by scoopseven in ColdFusion.add a comment
Getpagecontext().getResponse().sendRedirect(‘REMOTE PATH’);
GetPageContext().forward(“LOCAL PATH”);
XML Parsing Error IIS IE Firefox Coldfusion February 12, 2008
Posted by scoopseven in ColdFusion, IIS.add a comment
While loading a page in my browser, IE or FireFox, I kept getting this odd error message… “XML Parsing Error – Not Well Formed”. The odd thing was that I was loading a HMTL page, no XML. I tried changing the DocType tag, making my Javascript XML complaint (duh!) and I could not get this error to go away. I read something re: ASP causing the problem, so I disabled that in IIS, nothing. Turns out in a background process, I was calling a web service where the following code was used.
<cfheader name=”Content-Type” value=”text/xml; charset=utf-8″>
So CF had my browsers thinking that my non-XML / HTML page was in fact XML. Easy fix, I just added this tag as the first line in my HMTL page:
<cfheader name=”Content-Type” value=”text/html”>
And the world was right again
CFAPPLICATION November 25, 2007
Posted by scoopseven in ColdFusion.1 comment so far
Despite what CFMX 6.1 documentation says, I’ve recently learned that the CFAPPLICATION tag, located in application.cfm doesn’t like ambiguity. The problem manifests itself by throwing a “NullPointerException at ColdFusion.runtime.clientScopeKey” error in the exception log.
We have been specifying something like this for our CFAPPLICATION tag.
<cfapplication
name=”myApp”
sessionmanagement=”Yes”
sessiontimeout=”#CreateTimeSpan(0,0,30,0)#”
clientmanagement=”Yes”>
Because documentation states that if no “clientstorage” tag is specified in the cfapplication tag it will default to the setting specified in the client storage setting in the CF administrator section, which we have set to “Cookie”. Unfortunately, for busy apps, this doesn’t hold true. CF throws errors with the above cfapplication tag when the app gets busy enough, stating that it can’t find the clientstorage variable value.
So, for all your CFAPPLICATION tags in application.cfm, make sure you add the two additional variables below (clientstorage and setclientcookies) to remove any ambiguity.
<cfapplication
name=”myApp”
sessionmanagement=”yes”
sessiontimeout=”#CreateTimeSpan(0,0,30,0)#”
clientmanagement=”yes”
clientstorage=”Cookie”
setclientcookies=”yes”>