0

我想添加一个语言参数。假设用户点击英文,他只会得到英文的推文。现在我en默认使用,但我希望支持多种语言

function twitter_class()
{
    $this->realNamePattern = '/\((.*?)\)/';

    $this->searchURL = 'http://search.twitter.com/search.atom?lang=en&q=';
}

$ch= curl_init($this->searchURL . urlencode($q));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

我想从用户那里获得语言输入。

4

2 回答 2

3

您可以制作searchURL一些参数化的字符串函数:

$this->searchURL = function($language, $query) {
    $vars = array_map('urlencode', func_get_args() + array('', ''));        
    return vsprintf('http://search.twitter.com/search.atom?lang=%s&q=%s', $vars);
}

当你调用它时,这样做:

$ch = curl_init($this->searchURL($lang, $q));

希望这会有所帮助。


如果您没有 PHP 5.3,只需将其编写为标准类函数即可:

class twitter_class
{
    ...

    function twitter_class()
    {
        $this->realNamePattern = '/\((.*?)\)/';
    }

    function searchURL($language, $query) {
        $vars = array_map('urlencode', array($language, $query));
        return vsprintf('http://search.twitter.com/search.atom?lang=%s&q=%s', $vars);
    }

    ...

        $ch = curl_init($this->searchURL($lang, $q));
    ...
}
于 2012-04-17T12:03:18.547 回答
0
function twitter_class($lang="en")
{
    $this->realNamePattern = '/\((.*?)\)/';

    $this->searchURL = 'http://search.twitter.com/search.atom?lang='.$lang.'&q=';

}
于 2012-04-17T12:11:48.263 回答