上传图片后,我试图标记它,但上传图片后返回的"id“显然没有任何用处。
我想知道是否有人能够做到同样的事情?
function upload_image($file,$session,$message) {
$args = array(
'message' => $message,
);
$args[basename($file)] = '@' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$album_id.'/photos?access_token='.$session['access_token'];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
$result = json_decode($data,true);
$picture = $result['id'];
print_r($picture);
$json = 'https://api.facebook.com/method/photos.addTag?pid='.$picture.'&tag_text=Test&x=0&y=0&access_token='.$session['access_token'];
$ch = curl_init();
$url = $json;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$data = curl_exec($ch);
print_r($data);
}
发布于 2010-12-14 00:43:27
这似乎是新的Graph API的一个缺陷。通过一些实验和阅读,Facebook上的照片似乎有两种ID。
我还没有找到从一种ID转换到另一种ID的方法。如果你问我,这真是太愚蠢了。
我现在可以想到两种方法来通过API调用上传和标记照片:
使用photos.upload上传图像并使用photos.addTag.对其进行标记的
而且,似乎无论我传递给Graph API upload图像调用的是什么相册ID,它都会将上传的图像放入名为"Application Name Photos“的相册中。
还有一件事,就作用域/请求权限而言,我似乎需要publish_stream访问才能将上传的图像立即放到用户的帐户上。如果您没有publish_stream访问权限,则需要先获得用户的批准,然后他或她的朋友才能查看。您将需要user_photos access来获取方法2的用户照片列表。
希望这篇文章能帮到你。
相关链接:
的相同问题
发布于 2011-01-10 02:07:13
嘿,你可以用GRAPH API直接在上传上标记图片,参见下面的例子:这个方法为标签信息创建了一个数组,在这个例子中,这个方法变成了一个Facebook用户in的数组:
private function makeTagArray($userId) {
foreach($userId as $id) {
$tags[] = array('tag_uid'=>$id, 'x'=>$x,'y'=>$y);
$x+=10;
$y+=10;
}
$tags = json_encode($tags);
return $tags;
}
下面是调用GRAPH API上传图片的参数:
$arguments = array(
'message' => 'The Comment on this Picture',
'tags'=>$this->makeTagArray($this->getRandomFriends($userId)),
'source' => '@' .realpath( BASEPATH . '/tmp/'.$imageName),
);
下面是GRAPH API调用的方法:
public function uploadPhoto($albId,$arguments) {
//https://graph.facebook.com/me/photos
try {
$fbUpload = $this->facebook->api('/'.$albId.'/photos?access_token='.$this->facebook->getAccessToken(),'post', $arguments);
return $fbUpload;
}catch(FacebookApiException $e) {
$e;
// var_dump($e);
return false;
}
}
参数$albId包含一个来自Facebook相册的ID。
如果你想从一个相册中标记一个现有的图片,你可以使用这个方法:首先,我们需要来自REST API的正确的图片ID,在这个例子中,我们需要来自应用程序已经创建的相册的名称,或者使用这个应用程序的用户的名称。该方法返回此相册的最后一张上传图片的图片ID:
public function getRestPhotoId($userId,$albumName) {
try {
$arguments = array('method'=>'photos.getAlbums',
'uid'=>$userId
);
$fbLikes = $this->facebook->api($arguments);
foreach($fbLikes as $album) {
if($album['name'] == $albumName) {
$myAlbId = $album['aid'];
}
}
if(!isset($myAlbId))
return FALSE;
$arguments = array('method'=>'photos.get',
'aid'=>$myAlbId
);
$fbLikes = $this->facebook->api($arguments);
$anz = count($fbLikes);
var_dump($anz,$fbLikes[$anz-1]['pid']);
if(isset($fbLikes[$anz-1]['pid']))
return $fbLikes[$anz-1]['pid'];
else
return FALSE;
//var_dump($fbLikes[$anz-1]['pid']);
//return $fbLikes;
}catch(FacebookApiException $e) {
$e;
// var_dump($e);
return false;
}
}
现在,您已经从REST API获得了正确的图片ID,您可以调用REST API来标记此图片,$pid是来自getRestPhotoId方法的图片,$tag_uid是一个Facebook userId:
$json = 'https://api.facebook.com/method/photos.addTag?pid='.$pid.'&tag_uid='.$userId.'&x=50&y=50&access_token='.$this->facebook->getAccessToken();
$ch = curl_init();
$url = $json;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_GET, true);
$data = curl_exec($ch);
这一行非常重要: curl_setopt($ch,CURLOPT_GET,true);您必须使用CUROPT_GET而不是CUROPT_POST来添加一个抛出REST API的标记。
我希望这对你有帮助。
来自斯图尔特的Kay最好的祝福
https://stackoverflow.com/questions/4429349
复制相似问题