public
Last active

publish an update w/ image to Yahoo! using the PHP SDK

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
<?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
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.