Php 使用Graph/REST API上传后的Facebook标签图像
上传图像后,我试图标记它,但上传图像后返回的“id”显然没有用 我想知道是否有人也能做到这一点Php 使用Graph/REST API上传后的Facebook标签图像,php,facebook,Php,Facebook,上传图像后,我试图标记它,但上传图像后返回的“id”显然没有用 我想知道是否有人也能做到这一点 function upload_image($file,$session,$message) { $args = array( 'message' => $message, ); $args[basename($file)] = '@' . realpath($file); $ch = curl_init(); $url = 'https:/
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);
}
这似乎是新Graph API的一个缺陷。通过一些实验和阅读,Facebook上的照片似乎有两种ID
- 新建图形APIID。此ID仅在图形API调用中可用。由于photos.addTag是旧API的一部分,因此传递给它的ID无效
- 旧的REST APIID。此ID通过REST API调用检索,即照片。获取或照片。上载
- 旧RESTAPI
- 旧RESTAPI
- 图形API
- 和你的问题一样
- 新建图形APIID。此ID仅在图形API调用中可用。由于photos.addTag是旧API的一部分,因此传递给它的ID无效
- 旧的REST APIID。此ID通过REST API调用检索,即照片。获取或照片。上载
- 使用照片上载图像。上载并使用照片对其进行标记。添加标记。这可能是最干净的方法
- 使用示例中的Graph API调用上载图像,然后使用照片。get获取图像列表,通过迭代查找刚刚上载的图像。这是一种允许您维护Graph API调用的方法,但它充其量只是一种黑客行为 此外,似乎无论我传递给Graph API upload image调用的相册ID是什么,它都会将上传的图像放入名为“[应用程序名称]照片”的相册中 还有一件事,就范围/请求权限而言,我似乎需要publish\u stream访问权限才能让上传的图像立即在用户帐户上生效。如果您没有发布\u-stream访问权限,则图像需要获得用户的批准,他或她的朋友才能看到。您需要用户照片访问权限才能获取方法2的用户照片列表 希望这些信息对你有所帮助 相关链接:
- 旧RESTAPI
- 旧RESTAPI
- 图形API
- 和你的问题一样
- 这似乎是新图形API的一个缺陷。通过一些实验和阅读,Facebook上的照片似乎有两种ID
- 嘿,
您可以使用GRAPH API直接在上传上标记图片,
请参见下面的示例:
此方法为标记信息创建一个数组,在本例中,此方法将成为一个带有Facebook用户ID的数组:
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
如果要标记相册中的现有图片,可以使用以下方法:
首先,我们需要RESTAPI中正确的图片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用户ID:
$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添加标记
我希望这对你有帮助
斯图尔特向凯致以最良好的祝愿嘿,
您可以使用GRAPH API直接在上传上标记图片,
请参见下面的示例:
此方法为标记信息创建一个数组,在本例中,此方法将成为一个带有Facebook用户ID的数组:
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
如果要标记相册中的现有图片,可以使用以下方法:
首先,我们需要RESTAPI中正确的图片ID,在本例中,我们需要应用程序创建的相册或用户创建的相册中的名称