Hi everyone–
I’m having a bit of a conundrum here. When I do a track request, I get results. All is fine. But when I try to do a follow request, I get a 401 error. Thoughts?
public function start($_keywords = array(), $_user_ids = array())
{
if(!is_array($_keywords))
{
$_keywords = array($_keywords);
}
if(!is_array($_user_ids))
{
$_user_ids = array($_user_ids);
}
while(1)
{
$fp = fsockopen("ssl://stream.twitter.com", 443, $errno, $errstr, 30);
if (!$fp)
{
echo "ERROR: Twitter Stream Error: failed to open socket";
} else
{
//
// build the data and store it so we can get a length
//
$data = "";
if(count($_keywords))
{
$data .= 'track=' . rawurlencode(implode(",", $_keywords));
}
if(count($_user_ids))
{
if(count($_keywords))
{
$data .= "&";
}
$data .= 'follow=' . rawurlencode(implode(",", $_user_ids));
}
//$data .= "&count=20";
echo "DATA: \n\n".$data."\n\n";
//
// store the current timestamp
//
$this->m_oauth_timestamp = time();
//
// generate the base string based on all the data
//
$base_string = 'POST&' .
rawurlencode('https://stream.twitter.com/1.1/statuses/filter.json') . '&' .
rawurlencode('oauth_consumer_key=' . $this->m_oauth_consumer_key . '&' .
'oauth_nonce=' . $this->m_oauth_nonce . '&' .
'oauth_signature_method=' . $this->m_oauth_signature_method . '&' .
'oauth_timestamp=' . $this->m_oauth_timestamp . '&' .
'oauth_token=' . $this->m_oauth_token . '&' .
'oauth_version=' . $this->m_oauth_version . '&' .
$data);
echo "BASE STRING: \n\n".$base_string."\n\n" ;
//
// generate the secret key to use to hash
//
$secret = rawurlencode($this->m_oauth_consumer_secret) . '&' .
rawurlencode($this->m_oauth_token_secret);
//
// generate the signature using HMAC-SHA1
//
// hash_hmac() requires PHP >= 5.1.2 or PECL hash >= 1.1
//
$raw_hash = hash_hmac('sha1', $base_string, $secret, true);
//
// base64 then urlencode the raw hash
//
$this->m_oauth_signature = rawurlencode(base64_encode($raw_hash));
//
// build the OAuth Authorization header
//
$oauth = 'OAuth oauth_consumer_key="' . $this->m_oauth_consumer_key . '", ' .
'oauth_nonce="' . $this->m_oauth_nonce . '", ' .
'oauth_signature="' . $this->m_oauth_signature . '", ' .
'oauth_signature_method="' . $this->m_oauth_signature_method . '", ' .
'oauth_timestamp="' . $this->m_oauth_timestamp . '", ' .
'oauth_token="' . $this->m_oauth_token . '", ' .
'oauth_version="' . $this->m_oauth_version . '"';
//
// build the request
//
$request = "POST /1.1/statuses/filter.json HTTP/1.1\r\n";
$request .= "Host: stream.twitter.com\r\n";
$request .= "Authorization: " . $oauth . "\r\n";
$request .= "Content-Length: " . strlen($data) . "\r\n";
$request .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
$request .= $data;