Skip to content

Instantly share code, notes, and snippets.

@jgraup
Last active December 29, 2015 07:19
Show Gist options
  • Save jgraup/7634944 to your computer and use it in GitHub Desktop.
Save jgraup/7634944 to your computer and use it in GitHub Desktop.
CodePen.io - Pulls CodePen.io User Feed
<?php
/**
* CodePen.io user feed example
*/
include_once("codepen.php");
if ((isset($_REQUEST['format']) && $_REQUEST['format'] == 'xml') ||
(isset($_REQUEST['output']) && $_REQUEST['output'] == 'xml') ) {
header("Content-type: text/xml; charset=utf-8");
echo isset($_REQUEST['user'])
? CodePen::Feed($_REQUEST['user'])
: CodePen::Feed();
exit();
}
$data = isset($_REQUEST['user'])
? CodePen::FeedJSON($_REQUEST['user'])
: CodePen::FeedJSON();
if ($data)
{
if ($_REQUEST['callback']) {
// jsonp
header('Content-type: application/javascript');
echo $_REQUEST['callback'],'(', $data,');';
} else {
header('Content-type: application/json');
echo $data;
}
}
else
{
echo "Error", "";
}
<?php
/**
* CodePen.io Class
*/
class CodePen
{
/**
* @param string $user
* @return string url of user's feed
*/
public static function FeedUrl($user)
{
return "https://codepen.io/{$user}/activity/feed";
}
/**
* @param string $user (optional)
* @return string feed as string
*/
public static function Feed($user = "justgooddesign")
{
try {
return @file_get_contents(CodePen::FeedUrl($user));
}
catch (Exception $e) {
return $e->getMessage();
}
}
/**
* @param string $user (optional)
* @return SimpleXMLElement feed as XML object
*/
public static function FeedXML($user = "justgooddesign")
{
try {
return simplexml_load_file(CodePen::FeedUrl($user));
}
catch (Exception $e) {
return $e->getMessage();
}
}
/**
* @param string $user (optional)
* @return string feed as JSON
*/
public static function FeedJSON($user = "justgooddesign")
{
try {
$xml = @file_get_contents(CodePen::FeedUrl($user));
$xml = preg_replace('~\s*(<([^>]*)>[^<]*</\2>|<[^>]*>)\s*~','$1',$xml);
$xml = simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA);
return json_encode($xml);
}
catch (Exception $e) {
return $e->getMessage();
}
}
}
{
"@attributes": {
"version": "2.0"
},
"channel": {
"title": "Recent Activity on CodePen",
"link": "http://codepen.io/justgooddesign/activity/",
"description": {},
"item": [
{
"title": "\n          \n\n  \n\n\n\n\n\n\n\n\n  \n  MayconSouza\n\n    hearted your Pen\n    \n      \"Leaflet.js\"\n    \n\n\n\n\n\n        ",
"link": "http://codepen.io/justgooddesign/activity/",
"guid": "http://codepen.io/justgooddesign/activity/",
"author": "Just Good Design",
"description": "\n  <a href=\"http://codepen.io/MayconSouza/\" class=\"activity-avatar\"><img src=\"//gravatar.com/avatar/7e714ba044c8b008be1818b573ac8c9a?s=80\" alt=\"\" /></a><img src=\"http://codepen.io/images/activity/activity-heart.png\" /><p class=\"activity-sentence wrap-text\"><a href=\"http://codepen.io/MayconSouza\" class=\"activity-name\">MayconSouza</a><span class=\"activity-action\">hearted your Pen</span><a href=\"http://codepen.io/justgooddesign/details/HpExK\" class=\"activity-thing\">Leaflet.js</a></p>",
"pubDate": "2013-11-24T01:06:23-07:00"
}
]
}
}
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title>Recent Activity on CodePen</title>
<link>http://codepen.io/justgooddesign/activity/</link>
<description/>
<dc:language>en</dc:language>
<dc:creator>nospam@codepen.io</dc:creator>
<dc:rights>Copyright 2013</dc:rights>
<dc:date>2013-11-24T01:06:23-07:00</dc:date>
<item>
<title>
MayconSouza hearted your Pen "Leaflet.js"
</title>
<link>http://codepen.io/justgooddesign/activity/</link>
<guid>http://codepen.io/justgooddesign/activity/</guid>
<author>Just Good Design</author>
<description>
<![CDATA[
<a href="http://codepen.io/MayconSouza/" class="activity-avatar"> <img src="//gravatar.com/avatar/7e714ba044c8b008be1818b573ac8c9a?s=80" alt="" /> </a> <img src="http://codepen.io/images/activity/activity-heart.png" /> <p class="activity-sentence wrap-text"> <a href="http://codepen.io/MayconSouza" class="activity-name">MayconSouza</a> <span class="activity-action">hearted your Pen</span> <a href="http://codepen.io/justgooddesign/details/HpExK" class="activity-thing"> Leaflet.js </a> </p>
]]>
</description>
<pubDate>2013-11-24T01:06:23-07:00</pubDate>
</item>
</channel>
</rss>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment