所以我一直在使用几个月前的开源twitter php课程,突然之间,它昨晚开始让我犯错。你可以看到这里发生了什么:
www.campusmediawatch.org
它说它需要身份验证,但我会进行身份验证,自昨晚以来它已经工作了几个月。有任何想法吗?以下是功能:
public function getFriends($id = null, $page = null)
{
// build parameters
$aParameters = array();
if($page !== null) $aParameters['page'] = (int) $page;
// build url
$url = 'statuses/friends.xml';
if($id !== null) $url = 'statuses/friends/'. urlencode($id) .'.xml';
// do the call
$response = $this->doCall($url, $aParameters, true, false);
// convert into xml-object
$xml = @simplexml_load_string($response);
// validate
if($xml == false) throw new TwitterException('invalid body');
// init var
$aUsers = array();
// loop statuses
foreach ($xml->user as $user) $aUsers[] = $this->userXMLToArray($user);
// return
return (array) $aUsers;
}
以下是进行curl调用的代码:
private function doCall($url, $aParameters = array(), $authenticate = false, $usePost = true)
{
// redefine
$url = (string) $url;
$aParameters = (array) $aParameters;
$authenticate = (bool) $authenticate;
$usePost = (bool) $usePost;
// build url
$url = self::TWITTER_API_URL .'/'. $url;
// validate needed authentication
if($authenticate && ($this->getUsername() == '' || $this->getPassword() == '')) throw new TwitterException('No username or password was set.');
// rebuild url if we don't use post
if(!empty($aParameters) && !$usePost)
{
// init var
$queryString = '';
// loop parameters and add them to the queryString
foreach($aParameters as $key => $value) $queryString .= '&'. $key .'='. urlencode(utf8_encode($value));
// cleanup querystring
$queryString = trim($queryString, '&');
// append to url
$url .= '?'. $queryString;
}
// set options
$options[CURLOPT_URL] = $url;
$options[CURLOPT_PORT] = self::TWITTER_API_PORT;
$options[CURLOPT_USERAGENT] = $this->getUserAgent();
$options[CURLOPT_FOLLOWLOCATION] = true;
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut();
// should we authenticate?
if($authenticate)
{
$options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
$options[CURLOPT_USERPWD] = $this->getUsername() .':'. $this->getPassword();
}
// are there any parameters?
if(!empty($aParameters) && $usePost)
{
$var = '';
// rebuild parameters
foreach($aParameters as $key => $value) $var .= '&'. $key .'='. urlencode($value);
// set extra options
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = trim($var, '&');
// Probaly Twitter's webserver doesn't support the Expect: 100-continue header. So we reset it.
$options[CURLOPT_HTTPHEADER] = array('Expect:');
}
// init
$curl = curl_init();
// set options
curl_setopt_array($curl, $options);
// execute
$response = curl_exec($curl);
$headers = curl_getinfo($curl);
// fetch errors
$errorNumber = curl_errno($curl);
$errorMessage = curl_error($curl);
// close
curl_close($curl);
// validate body
$xml = @simplexml_load_string($response);
if($xml !== false && isset($xml->error)) throw new TwitterException((string) $xml->error);
// invalid headers
if(!in_array($headers['http_code'], array(0, 200)))
{
// should we provide debug information
if(self::DEBUG)
{
// make it output proper
echo '<pre>';
// dump the header-information
var_dump($headers);
// dump the raw response
var_dump($response);
// end proper format
echo '</pre>';
// stop the script
exit;
}
// throw error
throw new TwitterException(null, (int) $headers['http_code']);
}
// error?
if($errorNumber != '') throw new TwitterException($errorMessage, $errorNumber);
// return
return $response;
}
答案 0 :(得分:1)
我设法找到了你在wordpress插件库中使用的推特库。我希望它是一样的。
我使用它来玩我的帐户,我设法让它正常工作。我想您可能必须尝试登录您的Twitter帐户,因为只有当您的帐户用户名和密码无效时才会出现该错误(因此无法在Twitter上进行身份验证)
请尝试登录您的帐户,确保您的用户名和密码与初始化Twitter类的用户名和密码相同。
e.g。
$twitter_api = new Twitter(<username>, <password>);