Skip to content

Instantly share code, notes, and snippets.

@haet
Created June 25, 2012 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haet/2988518 to your computer and use it in GitHub Desktop.
Save haet/2988518 to your computer and use it in GitHub Desktop.
sneak.vu php-curl api
<?php
require_once('sneakvu.php');
?>
<html>
<head>
<title>
sneak.vu demo page
</title>
<?php
/*** define a thumbnail for your page on facebook ***********************/
/*** http://developers.facebook.com/docs/share/ ***********************/
$sneakvu = new Sneakvu('http://sneak.vu','fb','icon');
echo '<meta property="og:image" content="'.$sneakvu->getConnectionString().'" />';
?>
</head>
<body>
<?php
$sneakvu = new Sneakvu('http://sneak.vu','fb','icon');
echo '<h2>Facebook-Thumbnail: Sneakvu("http://sneak.vu","fb","icon")</h2>';
echo '<img src="'.$sneakvu->getConnectionString().'" title="sneak.vu facebook thumb">';
$sneakvu = new Sneakvu('http://sneak.vu');
echo '<h2>Small: Sneakvu("http://haet.at")</h2>';
echo '<img src="'.$sneakvu->getConnectionString().'" title="sneak.vu small"><br/>';
$sneakvu = new Sneakvu('http://sneak.vu','m');
echo '<h2>Medium: Sneakvu("http://sneak.vu","m")</h2>';
echo '<img src="'.$sneakvu->getConnectionString().'" title="sneak.vu medium">';
$sneakvu = new Sneakvu('http://sneak.vu','l');
echo '<h2>Large: Sneakvu("http://sneak.vu","l")</h2>';
echo '<img src="'.$sneakvu->getConnectionString().'" title="sneak.vu large">';
$sneakvu = new Sneakvu('http://sneak.vu','xl');
echo '<h2>XLarge: Sneakvu("http://sneak.vu","xl")</h2>';
echo '<img src="'.$sneakvu->getConnectionString().'" title="sneak.vu xlarge">';
$sneakvu = new Sneakvu('http://sneak.vu','full');
echo '<h2>Full-size: Sneakvu("http://sneak.vu","full")</h2>';
echo '<img src="'.$sneakvu->getConnectionString().'" title="sneak.vu full">';
?>
</body>
</html>
<?php
/*******************************************************************************
Copyright (c) 2012 haet webdevelopment
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
/**
* class Sneakvu
*
* a php class to simplify api calls to the sneak.vu api
*
*/
class Sneakvu{
protected $filetype;
protected $url;
protected $format;
protected $effect;
protected $apiUser = 'YOUR_SNEAK.VU_API_USER';
protected $apiKey = 'YOUR_SNEAK.VU_API_KEY';
public function __construct($url,$format='s',$effect='none'){
$this->url = $url;
$this->format = $format;
$this->effect = $effect;
}
/**
* getConnectionString()
* @param $response string: The response type (use "file" at the moment)
* @param $short boolean: shorten the url y/n
* @return string: returns either the full api request url with all
* parameters (including api credentials ) submitted in plain text or a
* short url referring silently to the full request
*
*/
public function getConnectionString($response="file",$short=true){
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off" )
$http = 'https://';
else
$http = 'http://';
$connectionString = $http.'sneak.vu/api/v1?apiUser='.$this->apiUser.'&apiKey='.$this->apiKey.'&url='.urlencode($this->url).'&format='.$this->format.'&effect='.$this->effect.'&response='.$response;
if($short){
$connectionString = $this->curlGetResult($http.'sneak.vu/shorten?url='.urlencode($connectionString));
}
return $connectionString;
}
/**
* getFile()
* @return image file (the file! not the path to the file!)
* jpg or png as needed by format and effect
* ** may be slow if the image has to be created for the first time
*/
public function getFile() {
$content = $this->curlGetResult($this->getConnectionString('file',false));
// Content-type image/jpg or image/png
header($this->filetype);
return $content;
}
/**
* curlGetResult($url)
* @returns result form url
*/
private function curlGetResult($url) {
$ch = curl_init();
$timeout = 20;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this,'readHeader') );
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* readHeader()
* helper function to identify the correct filetype of the requested file
*/
private function readHeader($ch, $header) {
if (strpos($header,'Content-Type:') !== false) {
$this->filetype = $header;
}
return strlen($header);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment