问题
I've inherited a site running Coldfusion, and I'm trying to get a twitter feed on the site. I've found a tool that seems to do the job, and I've registered my app with Twitter and I have my client and OAuth codes.
The problem is that I'm not familiar enough with Coldfusion to know exactly what code I need to put on the page in question to pull the tweets. This is the library I'm using: https://github.com/coldfumonkeh/monkehTweets
From what I can gather, my code should look something like this:
<cfinvoke
component = "component_name"
method="method_name"
returnvariable="return_variable">
<cfinvokeargument name="arg1" value="value">
<cfinvokeargument name="arg2" value="value">
</cfinvoke>
Unfortunately I have no idea what to put in any of those fields. There doesn't seem to be a list anywhere of the arguments OR what you should put in the name, method, and variable fields. All I need is to pull three recent tweets. This probably isn't a standard format for questions on here, but any help from someone who knows Coldfusion would be greatly appreciated. Thanks!
回答1:
I have written a very basic demo to pull out and display information from the authenticated user's timeline.
You need to start with the object instantiation, which it appears you have done: (obviously adding your own OAuth / Twitter details to the init method here)
<cfset objmonkehTweet = new monkehTweet.com.coldfumonkeh.monkehTweet(
consumerKey = '',
consumerSecret = '',
oauthToken = '',
oauthTokenSecret = '',
userAccountName = '',
parseResults = true
) />
Setting the parseResults value to true will convert the response into a struct or XML object that you can dump in ColdFusion and easily read the values.
Next, make a call to the getUserTimeline() method. If we don't pass any user id or screen name values in to the method call, it will access the timeline for the authenticated user:
<cfset arrStatus = objMonkehTweet.getUserTimeline() />
As no parameters were sent in regarding format, monkehTweet will return the default response as JSON (which, with parseResults set to true, will result in an array of structs).
Now we can start creating the loop. This is very basic, but will help you get up and running.
Define the maximum number of tweets to return:
<cfset totalTweets = 3 />
It's prudent to then check the length of the array (just in case we have less than the desired maximum number of results):
<cfif arrayLen(arrStatus) LT totalTweets>
<cfset totalTweets = arrayLen(arrStatus) />
</cfif>
Now define a loop, starting at 1 with a maximum of the totalTweet value:
<cfoutput>
<ul>
<cfloop from="1" to="#totalTweets#" index="tweet">
<cfset status = objMonkehTweet.entify(arrStatus[tweet]) />
<li>#arrStatus[tweet]['user']['name']#: #status#</li>
</cfloop>
</ul>
You can now access each specific tweet like so: arrStatus[tweet]. The monkehTweet object contains a helper function called entify which will convert any URLs, user mentions and hashtags into HTML URLs for display. Simply pass in the tweet object and it will return the formatted status for you. You can also reference any object within the response directly, as we have here with the user name value.
To see what is being returned, it's always best to dump out the entire response so that you can see what is available to access and use in your display:
<cfdump var="#arrStatus#" />
I hope that helps get you up and running.
来源:https://stackoverflow.com/questions/15285984/implement-monkehtweets-on-coldfusion-server