簡體   English   中英

使用PHP和cURL搶占Twitter朋友源

[英]Grabbing Twitter Friends Feed Using PHP and cURL

因此,為了回答我的最后一個問題,我正在努力抓取Twitter的好友供稿。 我遵循了一個教程來逐步編寫此腳本,因此我不確定它到底出了什么問題,也沒有看到任何錯誤消息。 從外殼保存之前,我從未真正使用過cURL,而且我對PHP還是很陌生,所以請多多包涵。

<html>
<head>
<title>Twitcap</title>
</head>
<body>
<?php
  function twitcap()
  {
    // Set your username and password
    $user = 'osoleve';
    $pass = '****';

    // Set site in handler for cURL to download
    $ch = curl_init("https://twitter.com/statuses/friends_timeline.xml");

    // Set cURL's option
    curl_setopt($ch,CURLOPT_HEADER,1); // We want to see the header
    curl_setopt($ch,CURLOPT_TIMEOUT,30); // Set timeout to 30s
    curl_setopt($ch,CURLOPT_USERPWD,$user.':'.$pass); // Set uname/pass
    curl_setopt($ch,CURLOPT_RETURNTRANSER,1); // Do not send to screen

    // For debugging purposes, comment when finished
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);

    // Execute the cURL command
    $result = curl_exec($ch);

    // Remove the header
    // We only want everything after <?
    $data = strstr($result, '<?');

    // Return the data
    $xml = new SimpleXMLElement($data);
    return $xml;
  }

  $xml = twitcap();
  echo $xml->status[0]->text;
?>
</body>
</html>

您實際上不需要“?>”之后的所有內容嗎?

$data = strstr($result,'?>');

另外,您是否正在使用免費的虛擬主機? 我曾經遇到過一個問題,我的托管服務提供商由於垃圾郵件而阻止了對Twitter的訪問。

請注意,如果使用strstr,后退字符串實際上將包括針串。 所以你必須從字符串中去除前兩個字符

我寧願推薦功能substr和strpos的組合!

順便說一句,我認為simplexml應該能夠處理此標頭,這意味着我認為這一步驟是不必要的!

此外,如果我打開網址,我不會看到類似的標題! 如果strstr找不到字符串,則返回false,因此當前腳本中沒有任何數據

而不是$data = strstr($result, '<?'); 嘗試這個:

if(strpos('?>',$data) !== false) {
$data = strstr($result, '?>');
} else {
$data = $result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM