Created
September 21, 2010 18:34
-
-
Save mchelen/590242 to your computer and use it in GitHub Desktop.
Klout Batch // Submits a list of names to Klout API to find influence and basic stats // Returns results in XML or JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// enter API key | |
$key = "****************"; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Klout Batch Users Query | |
// Submits a list of usernames to Klout API to find influence and basic stats | |
// Returns results in XML or JSON | |
// Limited to 5 usernames at once | |
// Author: Michael Chelen | |
// License: Creative Commons Zero Public Domain http://creativecommons.org/publicdomain/zero/1.0/ | |
// edit apikey.php to use your Klout API key | |
require "apikey.php"; | |
if ($_GET['users']=="") { | |
// no input provided, show html form | |
print ' | |
<html> | |
<head> | |
<title>Klout Batch Users Query</title> | |
</head> | |
<body> | |
Enter usernames seperated by commas or one per line:<br /> | |
<form name="input" method="get"> | |
<textarea cols="50" rows="20" name="users"></textarea> | |
<br /> | |
Result format: | |
<select name="format"> | |
<option selected="selected">xml</option> | |
<option>json</option> | |
</select> | |
<br /><input type="submit" value="Submit" /> | |
</form> | |
</body> | |
</html>'; | |
} | |
else { | |
// input received, run query | |
// set appropriate content-type header | |
if ($_GET['format'] == "json") { | |
$format = "json"; | |
header('Content-type: application/json'); | |
} | |
else { | |
$format = "xml"; | |
header ("Content-Type:text/xml"); | |
} | |
// base API url for selected format | |
$url = "http://api.klout.com/1/users/show.".$format."?key=".$key; | |
// replace new lines | |
$order = array("\r\n", "\n", "\r"); | |
$replace = ','; | |
$users = str_replace($order, $replace, $_GET['users']); | |
// break into array and encode for json query | |
$users = explode(",",$users); | |
$users = json_encode($users); | |
// create http query | |
$data = array ('users' => $users); | |
$data = http_build_query($data); | |
// issue post request | |
$result = do_post_request($url,$data); | |
// output result | |
print $result; | |
} | |
// from http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl | |
function do_post_request($url, $data, $optional_headers = null) | |
{ | |
$params = array('http' => array( | |
'method' => 'POST', | |
'content' => $data | |
)); | |
if ($optional_headers !== null) { | |
$params['http']['header'] = $optional_headers; | |
} | |
$ctx = stream_context_create($params); | |
$fp = @fopen($url, 'rb', false, $ctx); | |
if (!$fp) { | |
throw new Exception("Problem with $url, $php_errormsg"); | |
} | |
$response = @stream_get_contents($fp); | |
if ($response === false) { | |
throw new Exception("Problem reading data from $url, $php_errormsg"); | |
} | |
return $response; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment