[twitter-dev] Re: track syntax
I'm sorry Joel I keep getting a PHP timeout on the code you sent. I'll troubleshoot more and see if I can give you any more details (increase the maximum time, etc). Who knows, maybe its Twitter. Any other thoughts? I'll get back to you more with some detailed info. Are you able to get that exact code working on your server? On Aug 3, 2:12 pm, Joel Strellner wrote: > Hi Tom, > > I am not sure about XML, since I use JSON - it has a much lower > over-the-wire data size, and its easier to parse. > > Let me know if the code works for you. > > -Joel > > > > On Mon, Aug 3, 2009 at 10:55 AM, Tom Fitzgerald wrote: > > > I appreciate the reply Joel. I'll give it a try. I also tried just > > downloading from the stream api with the curl command line. However I > > kept getting 'malformed xml' errors. It was weird, each tweet would > > have the > thing with JSON format but it was a different error, still malformed. > > All I'm doing is > > > curlhttp://stream.twitter.com/spritzer.xml-uuser:pass > > > is the exact line I get before > > every status. If I manually clean up the XML (or JSON) it works great. > > > On Aug 2, 7:10 pm, Joel Strellner wrote: > > > Other than my username and password, this is an example that I know is > > > working: > > > > > > $count = 1; > > > $startparsing = false; > > > > $keyword_needles[] = 'twitter'; > > > $keyword_needles[] = 'keyword2'; > > > $keyword_needles[] = 'keyword3'; > > > $keyword_needles[] = 'keyword4'; > > > > // if your keywords have spaces, they must be urlencoded (twitter does > > > not support phrases, only the first keyword will be used, the space > > > character and after will be ignored) > > > foreach ($keyword_needles AS $i=>$needle) { > > > $keyword_needles[$i] = urlencode($needle); > > > > } > > > > $poststr = 'track=' . implode(',', $keyword_needles); > > > $fp = fsockopen("stream.twitter.com", 80, $errno, $errstr, 10); > > > if (!$fp) { > > > echo "$errstr ($errno)\n"; > > > > } else { > > > > $out = "POST /track.json HTTP/1.1\r\n"; > > > $out .= "Host: stream.twitter.com\r\n"; > > > $out .= "User-Agent: YourUserAgent\r\n"; > > > $out .= "Referer:http://yourdomain.com\r\n";; > > > $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; > > > $out .= "Authorization: Basic " . base64_encode > > > ("username:password")."\r\n"; > > > $out .= "Content-length: " . strlen($poststr) . "\r\n"; > > > $out .= "Connection: Close\r\n\r\n"; > > > $out .= $poststr . "\r\n\r\n"; > > > > fwrite($fp, $out); > > > while (!feof($fp)) { > > > $line = fgets($fp, 4096); > > > if ($startparsing) { > > > if (trim($line) != '') { > > > echo trim($line) . "\n"; > > > $tweet_obj = json_decode(trim($line)); > > > // do your stuff here > > > } > > > } > > > else { > > > // view the header lines: uncomment the below line > > > echo trim($line) . "\n"; > > > > $header_arr[] = $line; > > > $headercount = count($header_arr)-1; > > > > if (trim($header_arr[$headercount]) == '') { > > > $startparsing = true; > > > $count = 1; > > > unset($header_arr, $headercount); > > > } > > > } > > > if (trim($line) != '') $count++; > > > } > > > fclose($fp);} > > > > ?> > > > > The only changes I made was to echo things out and I added a keyword > > > that I was sure would have volume - twitter. It did take a little bit > > > of time to connect. I am assuming that that is because of their > > > current load though, and not the script. > > > > On Aug 2, 2:25 am, Tom Fitzgerald wrote: > > > > >Joel, > > > > > For some reason when I try your code I get a timeout error. Any > > > > suggestions? What you have is exactly what I'm looking for. It could > > > > really help me out a jam
[twitter-dev] Re: track syntax
I appreciate the reply Joel. I'll give it a try. I also tried just downloading from the stream api with the curl command line. However I kept getting 'malformed xml' errors. It was weird, each tweet would have the http://stream.twitter.com/spritzer.xml -uuser:pass is the exact line I get before every status. If I manually clean up the XML (or JSON) it works great. On Aug 2, 7:10 pm, Joel Strellner wrote: > Other than my username and password, this is an example that I know is > working: > > $count = 1; > $startparsing = false; > > $keyword_needles[] = 'twitter'; > $keyword_needles[] = 'keyword2'; > $keyword_needles[] = 'keyword3'; > $keyword_needles[] = 'keyword4'; > > // if your keywords have spaces, they must be urlencoded (twitter does > not support phrases, only the first keyword will be used, the space > character and after will be ignored) > foreach ($keyword_needles AS $i=>$needle) { > $keyword_needles[$i] = urlencode($needle); > > } > > $poststr = 'track=' . implode(',', $keyword_needles); > $fp = fsockopen("stream.twitter.com", 80, $errno, $errstr, 10); > if (!$fp) { > echo "$errstr ($errno)\n"; > > } else { > > $out = "POST /track.json HTTP/1.1\r\n"; > $out .= "Host: stream.twitter.com\r\n"; > $out .= "User-Agent: YourUserAgent\r\n"; > $out .= "Referer:http://yourdomain.com\r\n";; > $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; > $out .= "Authorization: Basic " . base64_encode > ("username:password")."\r\n"; > $out .= "Content-length: " . strlen($poststr) . "\r\n"; > $out .= "Connection: Close\r\n\r\n"; > $out .= $poststr . "\r\n\r\n"; > > fwrite($fp, $out); > while (!feof($fp)) { > $line = fgets($fp, 4096); > if ($startparsing) { > if (trim($line) != '') { > echo trim($line) . "\n"; > $tweet_obj = json_decode(trim($line)); > // do your stuff here > } > } > else { > // view the header lines: uncomment the below line > echo trim($line) . "\n"; > > $header_arr[] = $line; > $headercount = count($header_arr)-1; > > if (trim($header_arr[$headercount]) == '') { > $startparsing = true; > $count = 1; > unset($header_arr, $headercount); > } > } > if (trim($line) != '') $count++; > } > fclose($fp);} > > ?> > > The only changes I made was to echo things out and I added a keyword > that I was sure would have volume - twitter. It did take a little bit > of time to connect. I am assuming that that is because of their > current load though, and not the script. > > On Aug 2, 2:25 am, Tom Fitzgerald wrote: > > > > >Joel, > > > For some reason when I try your code I get a timeout error. Any > > suggestions? What you have is exactly what I'm looking for. It could > > really help me out a jam, thanks! > > > On Jul 27, 4:02 pm,JoelStrellner wrote: > > > > Here is a working example of how to do /track: > > > > $count = 1; > > > $startparsing = false; > > > > $keyword_needles[] = 'keyword1'; > > > $keyword_needles[] = 'keyword2'; > > > $keyword_needles[] = 'keyword3'; > > > $keyword_needles[] = 'keyword4'; > > > > // if your keywords have spaces, they must be urlencoded (twitter does not > > > support phrases, only the first keyword will be used, the space character > > > and after will be ignored) > > > foreach ($keyword_needles AS $i=>$needle) { > > > $keyword_needles[$i] = urlencode($needle); > > > > } > > > > $poststr = 'track=' . implode(',', $keyword_needles); > > > $fp = fsockopen("stream.twitter.com", 80, $errno, $errstr, 30); > > > if (!$fp) { > > > echo "$errstr ($errno)\n";} else { > > > > $out = "POST /track.json HTTP/1.1\r\n"; > > > $out .= "Host: stream.twitter.com\r\n"; > > > $out .= "User-Agent: YourUserAgent\r\n"; > > > $out .= "Referer:http://yourdomain.com\r\n";; > > > $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; > > >
[twitter-dev] Re: track syntax
Joel, For some reason when I try your code I get a timeout error. Any suggestions? What you have is exactly what I'm looking for. It could really help me out a jam, thanks! On Jul 27, 4:02 pm, Joel Strellner wrote: > Here is a working example of how to do /track: > > $count = 1; > $startparsing = false; > > $keyword_needles[] = 'keyword1'; > $keyword_needles[] = 'keyword2'; > $keyword_needles[] = 'keyword3'; > $keyword_needles[] = 'keyword4'; > > // if your keywords have spaces, they must be urlencoded (twitter does not > support phrases, only the first keyword will be used, the space character > and after will be ignored) > foreach ($keyword_needles AS $i=>$needle) { > $keyword_needles[$i] = urlencode($needle); > > } > > $poststr = 'track=' . implode(',', $keyword_needles); > $fp = fsockopen("stream.twitter.com", 80, $errno, $errstr, 30); > if (!$fp) { > echo "$errstr ($errno)\n";} else { > > $out = "POST /track.json HTTP/1.1\r\n"; > $out .= "Host: stream.twitter.com\r\n"; > $out .= "User-Agent: YourUserAgent\r\n"; > $out .= "Referer:http://yourdomain.com\r\n";; > $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; > $out .= "Authorization: Basic " . > base64_encode("username:password")."\r\n"; > $out .= "Content-length: " . strlen($poststr) . "\r\n"; > $out .= "Connection: Close\r\n\r\n"; > $out .= $poststr . "\r\n\r\n"; > > fwrite($fp, $out); > while (!feof($fp)) { > $line = fgets($fp, 4096); > if ($startparsing) { > if (trim($line) != '') { > //echo trim($line) . "\n"; > $tweet_obj = json_decode(trim($line)); > // do your stuff here > } > } > else { > // view the header lines: uncomment the below line > //echo trim($line) . "\n"; > > $header_arr[] = $line; > $headercount = count($header_arr)-1; > > if (trim($header_arr[$headercount]) == '') { > $startparsing = true; > $count = 1; > unset($header_arr, $headercount); > } > } > if (trim($line) != '') $count++; > } > fclose($fp); > > > > } > On Mon, Jul 27, 2009 at 11:18 AM, Joseph wrote: > > > I am trying to use the track streaming API, but I'm having trouble > > with the syntax. The example shows how to use a tracking list stored > > in a file in the format: track = word1, word2, etc.. > > > I tried the following (following a successful fsockopen call to > > stream.twitter.api: > > > POST /track.json track = Palin, #fubar HTTP/1.1 Host: > > stream.twitter.com User-Agent: UserAgent Authorization: Basic > > cGXybmFzc3TzZGV2OlBhcm5hMzT1Mzl4MDMz Connection: Close > > > and I am getting the following error code: HTTP/1.1 400 Bad Request > > > The actual relevant test PHP code is: > > > $fp = fsockopen($url, 80, $errno, $errstr, 30); > > if (!$fp) { > > echo "$errstr ($errno)\n"; > > } else { > > echo "file handle: ".$fp.""; > > $header = "POST /track.json track = $trackTerms HTTP/1.1\r\n"; > > $header .= "Host: stream.twitter.com\r\n"; > > $header .= "User-Agent: UserAgent\r\n"; > > $header .= "Authorization: Basic " . base64_encode($twitUsername . > > ':' . $twitPwd)."\r\n"; > > $header .= "Connection: Close\r\n\r\n"; > > echo $header.""; > > fwrite($fp, $header); > > $line = fgets($fp, 4096); > > echo $line; > > fclose($fp); > > }