我正在运行以下在互联网上找到的代码,以获取一堆带有特定主题标签的推文:
<cfset consumerKey = "MY_CONSUMER_KEY">
<cfset consumerSecret = "MY_CONSUMER_SECRET">
<cfset bearerToken = ToBase64(consumerKey & ":" & consumerSecret)>
<cfset authorization = "Basic " & bearerToken>
<cfhttp url="https://api.twitter.com/oauth2/token" method="post" charset="utf-8">
<cfhttpparam type="header" name="Authorization" value="#authorization#">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded;charset=UTF-8">
<cfhttpparam type="body" value="grant_type=client_credentials">
</cfhttp>
<cfset bearerTokenResponse = DeserializeJSON(cfhttp.fileContent)>
<cfset authorization2 = "Bearer " & bearerTokenResponse.access_token>
<cfhttp url="https://api.twitter.com/1.1/search/tweets.json" method="get" charset="utf-8">
<cfhttpparam type="header" name="Authorization" value="#authorization2#">
<cfhttpparam type="url" name="q" value="%23ColdFusion">
</cfhttp>
显然,我已将自己的钥匙放入消费者钥匙和钥匙中。 consumersecret变量。
{"statuses":[{"created_at":"Tue May 17 16:32:17 +0000 2016","id":732609696109080577,"id_str":"732609696109080577","text":"Well done Team Konstruct! \n\n#konstruct\n#pvd\n#suncity\n#dunhill https:\/\/t.co\/yRUPIXmQ6t","truncated":false,"entities":{"hashtags":[{"text":"konstruct","indices":[28,38]},{"text":"pvd","indices":[39,43]},{"text":"suncity","indices":[44,52]},{"text":"dunhill","indices":[53,61]}],"symbols":[],"user_mentions":[],"urls":[{"url":"https:\/\/t.co\/yRUPIXmQ6t","expanded_url":"http:\/\/fb.me\/3kyQTnmLe","display_url":"fb.me\/3kyQTnmLe","indices":[62,85]}]},"metadata":{"iso_language_code":"en","result_type":"recent"},"source":"\u003ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003eFacebook\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":238145671,"id_str":"238145671","name":"Vincent Viljoen","screen_name":"konstructdesign","location":"Brackenfell, Cape Town","description":"I design and build exhibition stands. I love it when my clients win awards for their stands and their brands stand out because of my creativity...","url":"http:\/\/t.co\/soGTwPlFhi","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/soGTwPlFhi","expanded_url":"http:\/\/konstruct.co.za","display_url":"konstruct.co.za","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":589,"friends_count":1973,"listed_count":18,"created_at":"Fri Jan 14 13:36:08 +0000 2011","favourites_count":0,"utc_offset":7200,"time_zone":"Pretoria","geo_enabled":true,"verified":false,"statuses_count":1863,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1465748472\/VW_1_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1465748472\/VW_1_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/238145671\/1369226061","profile_link_color":"009999","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}],"search_metadata":{"completed_in":0.015,"max_id":732609696109080577,"max_id_str":"732609696109080577","query":"%2523konstruct","refresh_url":"?since_id=732609696109080577&q=%2523konstruct&include_entities=1","count":15,"since_id":0,"since_id_str":"0"}}
我已经无休止地搜索了几周,我可以用这种方式以清晰的方式显示。所以例如假设我想在页面上输出,实际的推文,谁发布推文的句柄和日期 - 如何!这让我很生气,因为我确信我之前做过类似的事,但不记得怎么做了!
非常感谢提前。
Ĵ
答案 0 :(得分:1)
回答您的评论,您可以使用for循环在CFSCRIPT中循环遍历数组。
// from / to loop
for (i=1;i LTE ArrayLen(array);i=i+1) {
WriteOutput(array[i]);
}
OR
// for .. in loop
for (elem in array) {
WriteOutput(elem);
}
在你的情况下,它将是:
response = DeserializeJSON(cfhttp.fileContent);
for (status in response.statuses) {
WriteOutput(status.id);
}
我当然会通过一个函数运行struct的文本部分,用break标记替换新行,然后创建urls锚标记。
答案 1 :(得分:0)
之前我曾经处理过类似的东西。我得到的答案是将JSON放入一个数组并循环遍历它。
首先,我们需要我们的JSON与我们合作。我用了this online validator and formatter。使事情变得非常容易阅读。
我摆脱了最后一个元素search_metadata
,因为它是我所有整齐变量的另一个主要范围,我们无法做到这一点。要使用ColdFusion自动删除它,您可能需要使用SpanExcluding
函数。
所以你留下了这个:
[{"created_at":"Tue May 17 16:32:17 +0000 2016","id":732609696109080577,"id_str":"732609696109080577","text":"Well done Team Konstruct! \n\n#konstruct\n#pvd\n#suncity\n#dunhill https:\/\/t.co\/yRUPIXmQ6t","truncated":false,"entities":{"hashtags":[{"text":"konstruct","indices":[28,38]},{"text":"pvd","indices":[39,43]},{"text":"suncity","indices":[44,52]},{"text":"dunhill","indices":[53,61]}],"symbols":[],"user_mentions":[],"urls":[{"url":"https:\/\/t.co\/yRUPIXmQ6t","expanded_url":"http:\/\/fb.me\/3kyQTnmLe","display_url":"fb.me\/3kyQTnmLe","indices":[62,85]}]},"metadata":{"iso_language_code":"en","result_type":"recent"},"source":"\u003ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003eFacebook\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":238145671,"id_str":"238145671","name":"Vincent Viljoen","screen_name":"konstructdesign","location":"Brackenfell, Cape Town","description":"I design and build exhibition stands. I love it when my clients win awards for their stands and their brands stand out because of my creativity...","url":"http:\/\/t.co\/soGTwPlFhi","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/soGTwPlFhi","expanded_url":"http:\/\/konstruct.co.za","display_url":"konstruct.co.za","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":589,"friends_count":1973,"listed_count":18,"created_at":"Fri Jan 14 13:36:08 +0000 2011","favourites_count":0,"utc_offset":7200,"time_zone":"Pretoria","geo_enabled":true,"verified":false,"statuses_count":1863,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1465748472\/VW_1_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1465748472\/VW_1_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/238145671\/1369226061","profile_link_color":"009999","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}]
我不确定您个人希望如何处理此字符串,但我使用cfsavecontent
将其转换为名为Tweets
的变量。
然后:
<cfset ArrayOfStructs = deserializeJson(Tweets)>
现在它已经在列表中,我可以遍历它并获取我想要的所有信息。例如:
<cfoutput>
<cfloop array="#ArrayOfStructs#" index="i">
#i.created_at# <br/>
#i.id_str#
#i.text# <br/>
#i.user.name# <br/>
#i.user.screen_name# <br/>
#i.user.url# <br/>
</cfloop>
</cfoutput>
您可以直接从Twitter获取嵌入推文的代码,然后在网页上填写用户名和状态编号以及所有好东西,这正是我所做的!
这适用于1推文或1百万。
这对你有用吗?我使用ColdFusion 11。
编辑:只要您希望每次都使用完全相同的格式,就可以使用字符串函数解析search_metadata
元素。我不确定你是怎么做的,但我刚才把它扔到了一起:
<!---Replace the beginning of the search_metadata element with a unique character--->
<cfset Tweets2 = replace(Tweets,',"search_metadata',"|")>
<!---Use SpanExcluding function to eliminate everything past the unique character.--->
<cfset Tweets3 = SpanExcluding(Tweets2, '|')>
你仍然需要手动添加}
到最后但是那个学术界。你结束了我所做的相同的字符串,并且应该能够遍历它。