使用PHP和新的Twitter API [英] Using PHP and the new Twitter API
问题描述
目前我已经掌握了它的工作方式,推文只是作为简单的文本字符串输出。我的问题是我如何控制输出的HTML?我想要能够显示链接作为链接,如果一个标签或Web地址在推文内陈述。
以下是我的代码,它将字符串输出为我的页面中的推文:
function get_tweet(){
require'tmhOAuth.php';
需要'tmhUtilities.php';
$ tmhOAuth = new tmhOAuth(array(
'consumer_key'=>'secret',
'consumer_secret'=>'secret',
'user_token '=>'secret',
'user_secret'=>'secret',
'curl_ssl_verifypeer'=> false
));
$ b $ code = $ tmhOAuth-> request('GET',$ tmhOAuth-> url('1.1 / statuses / user_timeline'),array(
'screen_name'=> 'evanrichards',
'count'=>'1'));
$ response = $ tmhOAuth-> response ['response'];
$ tweets = json_decode($ response,true);
echo($ tweets [0] ['text']);
$ / code $ / pre
解决方案一些示例代码,用于替换链接,标签和使用php中的链接的标记。
$ tweet =@george check out http:// www.google.co.uk #google;
//将网址转换为< a>链接
$ tweet = preg_replace(/([\w] + \:\ / \ / [\w - ?& ;;#〜= \.\ / \ @] + $> $ 1< / a>,$ tweet);
//将hashtags转换为< a>链接
$ tweet = preg_replace(/#([A-Za-z0-9\ / \。] *)/,< a target = \_new\href = \\ \\http://twitter.com/search?q=$1\>#$ 1< / a>,$ tweet);
//将头衔转换为< a>链接
$ tweet = preg_replace(/ @([A-Za-z0-9\ / \。] *)/,< a href = \http://www.twitter .com / $ 1 \> @ $ 1< / a>,$ tweet);
echo $ tweet;
这给出了输出
< a href =http://www.twitter.com/george> @ george< / a>请检查< a target =_ blankhref =http://www.google.co.uk> http://www.google.co.uk< / a> < a target =_ newhref =http://twitter.com/search?q=google>#google< / a>
所以您可以将您的代码更改为
函数get_tweet(){
要求'tmhOAuth.php';
需要'tmhUtilities.php';
$ tmhOAuth = new tmhOAuth(array(
'consumer_key'=>'secret',
'consumer_secret'=>'secret',
'user_token '=>'secret',
'user_secret'=>'secret',
'curl_ssl_verifypeer'=> false
));
$ b $ code = $ tmhOAuth-> request('GET',$ tmhOAuth-> url('1.1 / statuses / user_timeline'),array(
'screen_name'=> 'evanrichards',
'count'=>'1'));
$ response = $ tmhOAuth-> response ['response'];
$ tweets = json_decode($ response,true);
$ tweet = $ tweets [0] ['text'];
$ tweet = preg_replace(/([\w] + \:\ / \ / [\w-?&;#〜= \.\ / \ @] + [\ w \\\ /])/,< a target = \_blank\href = \$ 1 \> $ 1< / a>,$ tweet);
$ tweet = preg_replace(/#([A-Za-z0-9\ / \。] *)/,< a target = \_new\href = \ http://twitter.com/search?q=$1\\">#$1</a>,$ tweet);
$ tweet = preg_replace(/ @([A-Za-z0-9\ / \。] *)/,< a href = \http://www.twitter。 com / $ 1 \> @ $ 1< / a>,$ tweet);
echo($ tweet);
}
我相信正则表达式可以被改进。
甚至更好的是,你可以将它分成它自己的函数。
function linkify_tweet($ tweet){
$ tweet = preg_replace(/([\w] + \:\ / \ / [\w - ?& ;; #〜= \.\ / \ @] + [\ w \ /])/,< a target = \_blank\href = \$ 1 \> $ 1< / a>,$ tweet);
$ tweet = preg_replace(/#([A-Za-z0-9\ / \。] *)/,< a target = \_new\href = \ http://twitter.com/search?q=$1\\">#$1</a>,$ tweet);
$ tweet = preg_replace(/ @([A-Za-z0-9\ / \。] *)/,< a href = \http://www.twitter。 com / $ 1 \> @ $ 1< / a>,$ tweet);
返回$ tweet;
函数get_tweet(){
要求'tmhOAuth.php';
需要'tmhUtilities.php';
$ tmhOAuth = new tmhOAuth(array(
'consumer_key'=>'secret',
'consumer_secret'=>'secret',
'user_token '=>'secret',
'user_secret'=>'secret',
'curl_ssl_verifypeer'=> false
));
$ b $ code = $ tmhOAuth-> request('GET',$ tmhOAuth-> url('1.1 / statuses / user_timeline'),array(
'screen_name'=> 'evanrichards',
'count'=>'1'));
$ response = $ tmhOAuth-> response ['response'];
$ tweets = json_decode($ response,true);
echo(linkify_tweet($ tweets [0] ['text']));
}
Because of the new Twitter API, I am using PHP to display 1 latest tweet on my webpage using PHP.
At the moment I have got it working so that the tweet is just outputted as a simple text string. My question is how do I control the HTML outputted? I want to be able to display the links as links if a hashtag or web address is stated within the tweet. How do I do this?
Here's my code so far that outputs the string as a tweet in my page:
function get_tweet() {
require 'tmhOAuth.php';
require 'tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'secret',
'consumer_secret' => 'secret',
'user_token' => 'secret',
'user_secret' => 'secret',
'curl_ssl_verifypeer' => false
));
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
'screen_name' => 'evanrichards',
'count' => '1'));
$response = $tmhOAuth->response['response'];
$tweets = json_decode($response, true);
echo($tweets[0]['text']);
}
解决方案 Here is some example code for replacing links, hashtags and attags with links in php
$tweet = "@george check out http://www.google.co.uk #google";
//Convert urls to <a> links
$tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);
//Convert hashtags to twitter searches in <a> links
$tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);
//Convert attags to twitter profiles in <a> links
$tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);
echo $tweet;
This gives the output
<a href="http://www.twitter.com/george">@george</a> check out <a target="_blank" href="http://www.google.co.uk">http://www.google.co.uk</a> <a target="_new" href="http://twitter.com/search?q=google">#google</a>
So you could change your code to
function get_tweet() {
require 'tmhOAuth.php';
require 'tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'secret',
'consumer_secret' => 'secret',
'user_token' => 'secret',
'user_secret' => 'secret',
'curl_ssl_verifypeer' => false
));
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
'screen_name' => 'evanrichards',
'count' => '1'));
$response = $tmhOAuth->response['response'];
$tweets = json_decode($response, true);
$tweet = $tweets[0]['text'];
$tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);
$tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);
$tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);
echo($tweet);
}
I'm sure the regex's could be improved though.
Or even better you can then split it out into it's own function.
function linkify_tweet($tweet) {
$tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);
$tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);
$tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);
return $tweet;
}
function get_tweet() {
require 'tmhOAuth.php';
require 'tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'secret',
'consumer_secret' => 'secret',
'user_token' => 'secret',
'user_secret' => 'secret',
'curl_ssl_verifypeer' => false
));
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
'screen_name' => 'evanrichards',
'count' => '1'));
$response = $tmhOAuth->response['response'];
$tweets = json_decode($response, true);
echo(linkify_tweet($tweets[0]['text']));
}
这篇关于使用PHP和新的Twitter API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!