Фейсбок(Facebook) АПИ(API) – за ъплоад на снимки
Posted on | August 5, 2008 | No Comments
След дълга липса на писания в този блог поради липсата на свободно време реших да запечатамедин код които ми отне 3 часа от живота и както винаги се оказва проблемът е бил в — ( 2-те тиренца )
.
Идеята на този класс е да може някои девел. за напред ако има проблем с ъплоад на снимки в фейсбоок да го види да си го копне – да попълни липсващите параметри и наий-вече да му спести работа
.
Лиценза е гну, държа само да се спомене че е моя работа …
ето го и кода
-
<?php
-
/**
-
* @author Elin Ahmedov
-
* @category facebook image upload api
-
* @copyright GNU
-
* @since 06.08.2008
-
* @version 0.1
-
*/
-
require_once './client/facebook.php';
-
class fbPhotoUpload
-
{
-
private $namespace = array();
-
-
/**
-
* class constructor
-
* @return fbPhotoUpload
-
*/
-
-
public function fbPhotoUpload()
-
{
-
return $this;
-
}
-
-
/**
-
* setting external variables
-
*
-
* @param string $name
-
* @param string $value
-
*/
-
-
public function setVars( $name, $value )
-
{
-
$this->namespace[$name] = $value;
-
}
-
-
/**
-
* collecting arguments for later signature generation
-
*
-
* @param string $sig__name
-
* @param string $sig__value
-
*/
-
-
private function collect__sig__( $sig__name, $sig__value )
-
{
-
$this->namespace['__sig__'][$sig__name] = $sig__value;
-
}
-
-
/**
-
* signature generation
-
*
-
* @param string $text
-
* @return string
-
*/
-
-
private function set__sig__( $text )
-
{
-
$sig = '';
-
ksort( $this->namespace['__sig__'] );
-
foreach( $this->namespace['__sig__'] as $name=>$value )
-
$sig .= $name.'='.$value;
-
$this->namespace['__sig__']['sig'] = md5( $sig.$this->namespace['__secret__'] );
-
return preg_replace( '/__sig__/i', $this->namespace['__sig__']['sig'], $text );
-
}
-
-
/**
-
* create top headers
-
*
-
*/
-
-
private function setReqsHeaders()
-
{
-
$this->namespace['headers'] = "POST ".$this->namespace['request_file']." HTTP/1.1\r\n";
-
$this->namespace['headers'] .= "Host: ".$this->namespace['domain']."\r\n";
-
$this->namespace['headers'] .= "User-Agent: Facebook Photo API PHP5 Client 1.0 ".phpversion()."\r\n";
-
$this->namespace['headers'] .= "Connection: close\r\n";
-
$this->namespace['headers'] .= "Content-type: multipart/form-data, boundary=".$this->namespace['boundary']."\r\n";
-
$this->namespace['headers'] .= "Content-Length: __cont_lenght__\r\n";
-
$this->namespace['headers'] .= "MIME-version: 1.0\r\n\r\n";
-
$this->namespace['headers'] = preg_replace( '/__cont_lenght__/i', strlen( $this->namespace['add.headers'] ), $this->namespace['headers'] ).$this->namespace['add.headers'];
-
}
-
-
/**
-
* create additional headers
-
*
-
* @param string $content
-
*/
-
-
private function setAdditonalHeaders( $content )
-
{
-
preg_match( '/(?P<fname>[^\/]*?(?P<fext>[^\.]*))$/i', $this->namespace['file_name'], $matches );
-
$this->namespace['add.headers'] = '–'.$this->namespace['boundary']."\r\n";
-
$this->namespace['add.headers'] .= "Content-Disposition: form-data; name=\"method\"\r\n\r\n";
-
$this->namespace['add.headers'] .= "facebook.photos.upload\r\n";
-
$this->namespace['add.headers'] .= '–'.$this->namespace['boundary']."\r\n";
-
$this->collect__sig__( 'method', 'facebook.photos.upload' );
-
$this->namespace['add.headers'] .= "Content-Disposition: form-data; name=\"v\"\r\n\r\n";
-
$this->namespace['add.headers'] .= "1.0\r\n";
-
$this->namespace['add.headers'] .= '–'.$this->namespace['boundary']."\r\n";
-
$this->collect__sig__( 'v', '1.0' );
-
$this->namespace['add.headers'] .= "Content-Disposition: form-data; name=\"api_key\"\r\n\r\n";
-
$this->namespace['add.headers'] .= $this->namespace['__api_key__']."\r\n";
-
$this->namespace['add.headers'] .= '–'.$this->namespace['boundary']."\r\n";
-
$this->collect__sig__( 'api_key', $this->namespace['__api_key__'] );
-
$this->namespace['add.headers'] .= "Content-Disposition: form-data; name=\"session_key\"\r\n\r\n";
-
$this->namespace['add.headers'] .= $this->namespace['__session_key__']."\r\n";
-
$this->namespace['add.headers'] .= '–'.$this->namespace['boundary']."\r\n";
-
$this->collect__sig__( 'session_key', $this->namespace['__session_key__'] );
-
$this->namespace['add.headers'] .= "Content-Disposition: form-data; name=\"call_id\"\r\n\r\n";
-
$this->namespace['add.headers'] .= $this->namespace['call_id']."\r\n";
-
$this->namespace['add.headers'] .= '–'.$this->namespace['boundary']."\r\n";
-
$this->collect__sig__( 'call_id', $this->namespace['call_id'] );
-
$this->namespace['add.headers'] .= "Content-Disposition: form-data; name=\"caption\"\r\n\r\n";
-
$this->namespace['add.headers'] .= $this->namespace['__caption__']."\r\n";
-
$this->namespace['add.headers'] .= '–'.$this->namespace['boundary']."\r\n";
-
$this->collect__sig__( 'caption', $this->namespace['__caption__'] );
-
$this->namespace['add.headers'] .= "Content-Disposition: form-data; name=\"aid\"\r\n\r\n";
-
$this->namespace['add.headers'] .= $this->namespace['__aid__']."\r\n";
-
$this->collect__sig__( 'aid', $this->namespace['__aid__'] );
-
$this->namespace['add.headers'] .= '–'.$this->namespace['boundary']."\r\n";
-
$this->namespace['add.headers'] .= "Content-Disposition: form-data; name=\"sig\"\r\n\r\n";
-
$this->namespace['add.headers'] .= "__sig__\r\n";
-
$this->namespace['add.headers'] .= '–'.$this->namespace['boundary']."\r\n";
-
$this->namespace['add.headers'] .= "Content-Disposition: form-data; filename=\"".$matches['fname']."\"\r\n";
-
$this->namespace['add.headers'] .= "Content-Type: image/".$matches['fext']."\r\n";
-
$this->namespace['add.headers'] .= "Content-Transfer-Encoding: binary\r\n\r\n";
-
$this->namespace['add.headers'] .= "$content\r\n";
-
$this->namespace['add.headers'] .= '–'.$this->namespace['boundary']."–\r\n";
-
$this->namespace['add.headers'] = $this->set__sig__( $this->namespace['add.headers'] );
-
-
}
-
-
/**
-
*
-
* @param string $url
-
* @return string
-
*/
-
-
public function upload( $url )
-
{
-
$this->namespace['domain'] = 'api.facebook.com';
-
$this->namespace['request_file'] = '/restserver.php';
-
$this->namespace['file_name'] = $url;
-
$this->namespace['call_id'] = microtime( true );
-
$this->namespace['boundary'] = md5( $this->namespace['call_id'] );
-
$content = file_get_contents( $this->namespace['file_name'] );
-
$this->setAdditonalHeaders( $content );
-
$this->setReqsHeaders();
-
// print "——————————————-\n";
-
// print $this->namespace['headers'];
-
// print "——————————————-\n";
-
$fh = fsockopen( $this->namespace['domain'], 80, $er1, $er2, 30 );
-
fwrite( $fh, $this->namespace['headers'] );
-
$answer = '';
-
while ( !feof( $fh ) )
-
$answer .= fgets( $fh, 1024 );
-
fclose( $fh );
-
// print $answer;
-
// print "——————————————-\n";
-
return '{my_fimages_ok}';
-
}
-
-
private function __destruct()
-
{
-
unset( $this );
-
}
-
}
-
header( 'Content-type: text/plain' );
-
$appapikey = 'your_api_key';
-
$appsecret = 'your_api_secret';
-
$facebook = new Facebook($appapikey, $appsecret);
-
$user_id = $facebook->require_login();
-
$fbphu = new fbPhotoUpload;
-
$fbphu->setVars( '__api_key__', $appapikey );
-
$fbphu->setVars( '__secret__', $appsecret );
-
$fbphu->setVars( '__session_key__', $facebook->api_client->session_key );
-
$fbphu->setVars( '__aid__', '__album_id__' );
-
$fbphu->setVars( '__caption__', '__image_title__' );
-
print $fbphu->upload( 'http://'__image_address__ );
-
?>
Category: PHP
Comments
Leave a Reply