示例Twitter应用程序

时间:2010-03-25 10:52:01

标签: php

我在localhost上安装了WAMP并运行PHP脚本。我启用了cURL。这是我的代码。

<?php 
function updateTwitter($status)
{ 
 // Twitter login information 
 $username = 'xxxxx'; 
 $password = 'xxxxx';
 // The url of the update function 
 $url = 'http://twitter.com/statuses/update.xml'; 
 // Arguments we are posting to Twitter 
 $postargs = 'status='.urlencode($status); 
 // Will store the response we get from Twitter 
 $responseInfo=array(); 
 // Initialize CURL 
 $ch = curl_init($url);
 // Tell CURL we are doing a POST 
 curl_setopt($ch, CURLOPT_PROXY,"localhost:80");
 curl_setopt ($ch, CURLOPT_POST, true); 
 // Give CURL the arguments in the POST 
 curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
 // Set the username and password in the CURL call 
 curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password); 
 // Set some cur flags (not too important) 
 curl_setopt($ch, CURLOPT_VERBOSE, 1); 
 curl_setopt($ch, CURLOPT_NOBODY, 0); 
 curl_setopt($ch, CURLOPT_HEADER, 0); 
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 // execute the CURL call 
 $response = curl_exec($ch); 
 if($response === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors<br/>';
}

 // Get information about the response 
 $responseInfo=curl_getinfo($ch); 
 // Close the CURL connection curl_close($ch);
 // Make sure we received a response from Twitter 
 if(intval($responseInfo['http_code'])==200){ 
  // Display the response from Twitter 
  echo $response; 
 }else{ 
  // Something went wrong 
  echo "Error: " . $responseInfo['http_code']; 
 } 
curl_close($ch);
 }

updateTwitter("Just finished a sweet tutorial on http://brandontreb.com");

?>

这是我的输出

Operation completed without any errors
Error: 404

这是错误404的含义

404 Not Found:请求的URI无效或请求的资源(例如用户)不存在。

请帮忙。

1 个答案:

答案 0 :(得分:2)

您需要twitter网址末尾的格式,有关详细信息,请参阅twitter api文档:

http://apiwiki.twitter.com/Twitter-API-Documentation

代理可能也会引起您的问题,请删除此行:

curl_setopt($ch, CURLOPT_PROXY,"localhost:80");