dongyiyu3953 2016-03-18 02:25
浏览 50

使用API​​将T.co替换为完整的Twitter链接

I am currently using the EpiTwitter API to grab Tweets and store them in the database however I want to replace any 'T.co' links in a given tweet and replace them with the full links. The regex is where I need a bit of help for determining how to replace the link.

Twitter returns the URL's via the urls entity.

"urls": [{
  "url": "https:\/\/t.co\/XdXRudPXH5",
  "expanded_url": "https:\/\/blog.twitter.com\/2013\/rich-photo-experience-now-in-embedded-tweets-3",
  "display_url": "blog.twitter.com\/2013\/rich-phot\u2026",
  "indices": [80, 103]
}],

I am currently calling the API and looping through the tweets like so:

$resp = $twitterObj->get('/statuses/user_timeline.json', array('screen_name'=>$row['screenname']));

    foreach($resp->response as $tweet)
    {                       
        foreach($tweet['entities']['urls'] as $url_type) 
        {
            if(preg_match('/https:\/\/t.co\/[^\s]+/', $url_type['url']))
            {
                 $processedtweet = str_replace($url_type['url'], $url_type['expanded_url'], $tweet['text']);        
            }                           
        }
    }

The regex for checking it is a T.co link and replacing it with that links expanded_url or display_url entity is where I need help as the preg_match statement isn't evaluating the T.co link correctly.

Edit I tested this with the link: https://t.co/XdXRudPXH5 and it evaluates correctly however the twitter API shows it with the backslashes throughout the link. Will they be required by the time my code comes around to reading the url? Basically will my regex have to take all the backslashes into account.

Thanks.

  • 写回答

1条回答 默认 最新

  • dtr32787 2016-03-18 03:05
    关注

    I'll use strpos instead of preg_match

    if ( strpos(url_type['url'],'t.co') !== false) 
    
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备