TweetWorks API Code To Post To A Group

  1. <?php
  2. function sendToTweetworks($uri, $username, $password, $data){
  3. $host = 'tweetworks.com';
  4. $port = 80;
  5. //$uri = '/groups/search.xml';
  6. $timeout = 10;
  7.  
  8. $login = base64_encode( $username
  9. .':'
  10. .strip_tags(stripslashes($password)) );
  11.  
  12. // connect
  13. $r_error_code = null;
  14. $r_error_text = null;
  15. $fp = @fsockopen( $host, $port, $r_error_code, $r_error_text, $timeout );
  16. if( !$fp )
  17. return false;
  18.  
  19. // send
  20. $content = "data[key]=myapikey&" . $data;
  21. @fwrite( $fp, "POST $uri HTTP/1.1\r\n" );
  22. @fwrite( $fp, "Host: $host:$port\r\n" );
  23. @fwrite( $fp, "Authorization: Basic $login\r\n" );
  24. @fwrite( $fp, "User-Agent: wordpress-twitterposts\r\n" );
  25. @fwrite( $fp, "Content-type: application/x-www-form-urlencoded\r\n" );
  26. @fwrite( $fp, "Content-length: ".strlen($content)."\r\n" );
  27. @fwrite( $fp, "Connection: close\r\n" );
  28. @fwrite( $fp, "\r\n" );
  29. @fwrite( $fp, $content );
  30.  
  31. // receive
  32. $response = '';
  33. while( !@feof($fp) ) {
  34. $response.= @fread( $fp, 255 );
  35. }
  36.  
  37. // close
  38. @fclose( $fp );
  39.  
  40. // explode response
  41. $response = explode( "\r\n\r\n", trim($response) );
  42. $content = $response[1];
  43. //print $content;
  44. $header = explode( "\r\n", $response[0] );
  45. $headerarray = array();
  46. foreach( $header as $headerline ) {
  47. $seperator = ": ";
  48. $seperator_pos = strpos( $headerline, $seperator );
  49. $key = substr( $headerline, 0, $seperator_pos );
  50. $value = substr( $headerline, $seperator_pos+strlen($seperator) );
  51. //print $key . ": " . $value . "<br />";
  52. $headerarray[ strtolower(trim($key)) ] = strtolower(trim($value));
  53. }
  54. $header = $headerarray;
  55.  
  56. // return success
  57. if( $header['status']=='200 ok' )
  58. return true;
  59.  
  60. // set error code
  61. $error = array();
  62. preg_match( '/(.*)<error>(.*)<\/error>(.*)/s', $content, $error );
  63. $r_error_code = $header['status'];
  64. $r_error_text = $error[2];
  65.  
  66. // return error
  67. return false;
  68. }
  69.  
  70. $data = 'data[Post][body]=testing commentwitter in Tweetworks&data[Post][groupId]=1935&data[Post][sendToTwitter]=1';
  71. $uri = '/posts/add.xml';
  72. $username = 'bish_test';
  73. $password = '*******';
  74.  
  75. sendToTweetworks($uri, $username, $password, $data)
  76.  
  77. ?>
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Send To Twitter What's CommenTwitter?