[twitter-dev] Send status update with OAuth/PHP/cURL

2011-01-27 Thread Mike Jones
I was using the following code to hook to my twitter account but now
since I was using basic authentication instead of OAuth I am not
tweeting anymore. Does anyone know of the code of samples that use
OAuth with cURL/PHP?

?


function make_jmp_url($url,$login,$appkey,$format = 'xml',$version =
'2.0.1') {
//create the URL
$jmp = 'http://api.j.mp/shorten?version='.
$version.'longUrl='.urlencode($url).'login='.$login.'apiKey='.
$appkey.'format='.$format;

//get the url
$response = file_get_contents($jmp);

//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://j.mp/'.$xml-results-nodeKeyVal-hash;
}
}

function tweet($status) {

$username = 'test';
$password = 'test';

if ($status) {
$tweetUrl = 'http://www.twitter.com/statuses/update.xml';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $tweetUrl);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, status=$status);
curl_setopt($curl, CURLOPT_USERPWD, $username:$password);

$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);

if ($resultArray['http_code'] == 200) {
$msg += ' and was Tweeted!';
}

curl_close($curl);
}
}

$tweet = '';

$longURL = 'http://test.com' . $row-id;
$shortURL = make_jmp_url($longURL, 'username', 'apikey', 'json');

$tweet .= urlencode( $row-title );

if (strlen($tweet)  119) {
// shorten status update to fit in 140 with URL
$tweet = substr($tweet, 0, 116 ) . '...';
}

$tweet .= ' - ';
$tweet .= $shortURL;

tweet($tweet);

?

-- 
Twitter developer documentation and resources: http://dev.twitter.com/doc
API updates via Twitter: http://twitter.com/twitterapi
Issues/Enhancements Tracker: http://code.google.com/p/twitter-api/issues/list
Change your membership to this group: 
http://groups.google.com/group/twitter-development-talk


Re: [twitter-dev] Send status update with OAuth/PHP/cURL

2011-01-27 Thread Peter Denton
Abraham's oAuth library and examples work pretty much out of the box.
https://github.com/abraham/twitteroauth

If you need any help, please feel free to ask me off the list.

regards
Peter

On Thu, Jan 27, 2011 at 12:59 PM, Mike Jones mikejo...@integer.com wrote:

 I was using the following code to hook to my twitter account but now
 since I was using basic authentication instead of OAuth I am not
 tweeting anymore. Does anyone know of the code of samples that use
 OAuth with cURL/PHP?

 ?


 function make_jmp_url($url,$login,$appkey,$format = 'xml',$version =
 '2.0.1') {
//create the URL
$jmp = 'http://api.j.mp/shorten?version='.
 $version.'longUrl='.urlencode($url).'login='.$login.'apiKey='.
 $appkey.'format='.$format;

//get the url
$response = file_get_contents($jmp);

//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://j.mp/'.$xml- http://j.mp/%27.$xml-
 results-nodeKeyVal-hash;
}
 }

 function tweet($status) {

$username = 'test';
$password = 'test';

if ($status) {
$tweetUrl = 'http://www.twitter.com/statuses/update.xml';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $tweetUrl);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, status=$status);
curl_setopt($curl, CURLOPT_USERPWD, $username:$password);

$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);

if ($resultArray['http_code'] == 200) {
$msg += ' and was Tweeted!';
}

curl_close($curl);
}
 }

$tweet = '';

$longURL = 'http://test.com' . $row-id;
$shortURL = make_jmp_url($longURL, 'username', 'apikey', 'json');

$tweet .= urlencode( $row-title );

if (strlen($tweet)  119) {
// shorten status update to fit in 140 with URL
$tweet = substr($tweet, 0, 116 ) . '...';
}

$tweet .= ' - ';
$tweet .= $shortURL;

tweet($tweet);

 ?

 --
 Twitter developer documentation and resources: http://dev.twitter.com/doc
 API updates via Twitter: http://twitter.com/twitterapi
 Issues/Enhancements Tracker:
 http://code.google.com/p/twitter-api/issues/list
 Change your membership to this group:
 http://groups.google.com/group/twitter-development-talk




-- 
Peter Denton
Co-Founder, Product Marketing
www.mombo.com
cell: (206) 427-3866
twitter @Mombo_movies
twitter - personal: @petermdenton

-- 
Twitter developer documentation and resources: http://dev.twitter.com/doc
API updates via Twitter: http://twitter.com/twitterapi
Issues/Enhancements Tracker: http://code.google.com/p/twitter-api/issues/list
Change your membership to this group: 
http://groups.google.com/group/twitter-development-talk


Re: [twitter-dev] Send status update with OAuth/PHP/cURL

2011-01-27 Thread Abraham Williams
Using TwitterOAuth you basically need three lines of code to get started.

https://gist.github.com/564882

Abraham
-
Abraham Williams | Hacker Advocate | abrah.am
@abraham https://twitter.com/abraham | github.com/abraham | blog.abrah.am
This email is: [ ] shareable [x] ask first [ ] private.



On Thu, Jan 27, 2011 at 13:49, Peter Denton petermden...@gmail.com wrote:

 Abraham's oAuth library and examples work pretty much out of the box.
 https://github.com/abraham/twitteroauth

 If you need any help, please feel free to ask me off the list.

 regards
 Peter


 On Thu, Jan 27, 2011 at 12:59 PM, Mike Jones mikejo...@integer.comwrote:

 I was using the following code to hook to my twitter account but now
 since I was using basic authentication instead of OAuth I am not
 tweeting anymore. Does anyone know of the code of samples that use
 OAuth with cURL/PHP?

 ?


 function make_jmp_url($url,$login,$appkey,$format = 'xml',$version =
 '2.0.1') {
//create the URL
$jmp = 'http://api.j.mp/shorten?version='.
 $version.'longUrl='.urlencode($url).'login='.$login.'apiKey='.
 $appkey.'format='.$format;

//get the url
$response = file_get_contents($jmp);

//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://j.mp/'.$xml- http://j.mp/%27.$xml-
 results-nodeKeyVal-hash;
}
 }

 function tweet($status) {

$username = 'test';
$password = 'test';

if ($status) {
$tweetUrl = 'http://www.twitter.com/statuses/update.xml';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $tweetUrl);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, status=$status);
curl_setopt($curl, CURLOPT_USERPWD, $username:$password);

$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);

if ($resultArray['http_code'] == 200) {
$msg += ' and was Tweeted!';
}

curl_close($curl);
}
 }

$tweet = '';

$longURL = 'http://test.com' . $row-id;
$shortURL = make_jmp_url($longURL, 'username', 'apikey', 'json');

$tweet .= urlencode( $row-title );

if (strlen($tweet)  119) {
// shorten status update to fit in 140 with URL
$tweet = substr($tweet, 0, 116 ) . '...';
}

$tweet .= ' - ';
$tweet .= $shortURL;

tweet($tweet);

 ?

 --
 Twitter developer documentation and resources: http://dev.twitter.com/doc
 API updates via Twitter: http://twitter.com/twitterapi
 Issues/Enhancements Tracker:
 http://code.google.com/p/twitter-api/issues/list
 Change your membership to this group:
 http://groups.google.com/group/twitter-development-talk




 --
 Peter Denton
 Co-Founder, Product Marketing
 www.mombo.com
 cell: (206) 427-3866
 twitter @Mombo_movies
 twitter - personal: @petermdenton


  --
 Twitter developer documentation and resources: http://dev.twitter.com/doc
 API updates via Twitter: http://twitter.com/twitterapi
 Issues/Enhancements Tracker:
 http://code.google.com/p/twitter-api/issues/list
 Change your membership to this group:
 http://groups.google.com/group/twitter-development-talk


-- 
Twitter developer documentation and resources: http://dev.twitter.com/doc
API updates via Twitter: http://twitter.com/twitterapi
Issues/Enhancements Tracker: http://code.google.com/p/twitter-api/issues/list
Change your membership to this group: 
http://groups.google.com/group/twitter-development-talk


[twitter-dev] Send status update with OAuth/PHP/cURL

2009-04-21 Thread Doug D

I've been digging in to the Twitter OAuth stuff and am able to sign
into my site with the Twitter authentication, but I'm wondering if
there is a way to use the OAuth credentails to send a status update
using PHP and cURL. The code below sends an update, but only when a
static username:password is filled in.

My goal it to update (your) Twitter status from my site once you are
authenticated by Twitter. Any thoughts or tutorials? Thanks!

// Set username and password
$username = 'name';
$password = 'password';

// The message you want to send
$message = 'My status update';

// The twitter API address
$url = 'http://twitter.com/statuses/update.xml';

// Alternative JSON version
// $url = 'http://twitter.com/statuses/update.json';

// Set up and execute the curl process
$curl_handle = curl_init();

curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, status=$message);
curl_setopt($curl_handle, CURLOPT_USERPWD, $username:$password);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);


// check for success or failure
if (empty($buffer)) {
echo 'message';
} else {
echo 'success';
}