Skip to content

Instantly share code, notes, and snippets.

@ydn
Created January 23, 2010 01:18
Show Gist options
  • Save ydn/284358 to your computer and use it in GitHub Desktop.
Save ydn/284358 to your computer and use it in GitHub Desktop.
publish an update w/ image to Yahoo! using the PHP SDK
<?php
/*
Purpose:
This script creates an oauth access token for you, and then
allows you to use that token to publish an update (that
includes a thumbnail picture) w/ the Yahoo! Updates API
Prerequisites
* A server with PHP 5
* A valid domain for that server
* An OAuth app from the Yahoo! Dev. Network:
http://developer.apps.yahoo.com
* The Yahoo! PHP SDK:
http://developer.yahoo.com/social/sdk/index.html#php
Usage:
1. Put this script in a file on your server
2. Edit the include path for the Yahoo! PHP SDK
3. Edit the definitions of your app's key, secret, and id
4. Browse to the url of this script, log in to Yahoo!,
click the button, and then browse to profiles.yahoo.com to
see the new update
*/
//ref: http://developer.yahoo.com/social/sdk/index.html#php
require_once("../../yahoo-yos-social-php-1fe1b43/lib/Yahoo.inc");
//define constants
define('KEY', '');
define('SECRET', '');
define('APPID', '');
$session = YahooSession::requireSession(KEY, SECRET, APPID);
if ('true' == $_GET['post']) {
//ref: YahooUser::insertUpdate() in Yahoo.inc
//ref: http://developer.yahoo.com/social/rest_api_guide/Single-update-resource.html
$source = 'APP.'.APPID;
$suid = 'ugc';
$request_url = sprintf(
"http://%s/v1/user/%s/updates/%s/%s",
$YahooConfig["UPDATES_WS_HOSTNAME"],
$session->guid,
$source,
urlencode($suid)
);
$update_body = array(
'updates' => array(
array(
"class" => "app",
"collectionType" => "guid",
"collectionID" => $session->guid,
"type" => "appActivity",
//cast to string to avoid "...missing Required field: [pubDate..." error
"pubDate" => (string) time(),
"source" => $source,
"suid" => $suid,
"description" => 'The time is now '.date("g:i a"),
"title" => 'foo title',
"link" => 'http://en.wikipedia.org/wiki/Haiku#Examples',
"imgURL" => "http://farm1.static.flickr.com/125/338573258_6576d11c64_m.jpg?",
"imgWidth" => "240",
"imgHeight" => "150"
)
)
);
$update_body_json = json_encode($update_body);
//check output on http://profiles.yahoo.com/
$response = $session->client->put($request_url, "application/json", $update_body_json);
header('content-type: text/javascript');
//response returned as json, so just pass it along for sanity checking
echo 'handleResponse('.$response['responseBody'].');';
} else {
?>
<button id="button">click to post update</button>
<script>
var handleResponse = function (data) {
document.getElementById('button').innerHTML='posted :)';
if (console) {
console.log(data);
}
},
handleClick = function (e) {
var script = document.createElement('script');
script.src = '?post=true';
document.body.appendChild(script);
};
document.getElementById('button').onclick = handleClick;
</script>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment