像Facebook的外部使用Graph Api - 的例子 [英] Like a Facebook Post externally using Graph Api - example

查看:114
本文介绍了像Facebook的外部使用Graph Api - 的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我一个例子,通过一个身份验证的用户从一个外部应用程序发布一个Facebook的帖子?说我有一个从Facebook上发帖的资料,我想创建一个按钮,让用户喜欢我应用程序的一个帖子。点击这个按钮,我怎么可以最好地创建这样的,以便在Facebook上更新用户的帖子?



我的想法是,我可以做一个javascript onclick函数,这样一个php脚本就像 - $ facebook-> api(/ $ id / likes,POST); ,但是我需要重新初始化该页面的Facebook,然后才能进行此呼叫?或发送我的访问令牌等?这对我这个服务器端方法感到困惑。点击后如何拨打电话?一个例子真的很有帮助!谢谢提前!

解决方案

好的,所以终于找到了一个体面的解决方案,没有卷曲。这是我在我的ajax函数onclick中调用的脚本:

 <?php 
$ access_token = $ _GET [ '令牌'];

$ post_id = $ _GET ['id'];

函数do_post_request($ url,$ data,$ optional_headers = null)
{
$ params = array('http'=> array(
' '=>'POST',
'content'=> $ data
));
if($ optional_headers!== null){
$ params ['http'] ['header'] = $ optional_headers;
}
$ ctx = stream_context_create($ params);
$ fp = @fopen($ url,'rb',false,$ ctx);
if(!$ fp){
echoerror;
}
$ response = @stream_get_contents($ fp);
if($ response === false){
throw new Exception(读取数据的问题);
}
return $ response;
}

$ data =access_token =$ access_token;

$ url ='https://graph.facebook.com/'.$post_id.'/likes';

$ result = do_post_request($ url,$ data,$ optional_headers = null);
?>

另外在我测试过的工作代码中,$ post_id的用户标识的头文件如下: SOURCEUSERID_POSTID



从这篇文章的组合得到这个解决方案 - http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/



和这一个 - 喜欢Facebook上的帖子图表api



希望这有助于别人出去!


Could someone please give me an example of posting a like for a facebook post from an external app with an authenticated user?

Say I have a feed pulling posts from facebook, and I want to create a button that lets the user like a post from my app. On click of this button, how could I best create that like so that the post is updated on Facebook with the user's like?

My thoughts are that I could make a javascript onclick function that ajaxes to a php script that does something like - $facebook->api("/$id/likes",'POST'); , but do I need to re-initialize facebook from that page before I can make this call? or send my access token etc.? This is what kind of confuses me about this server-side method. How do you make this call upon click? An example would be really helpful! Thanks in advance!!

解决方案

Okay so finally found a decent solution, and without curl. This is the script I call to in my ajax function onclick:

<?php
$access_token = $_GET['token'];

$post_id = $_GET['id'];

function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
          'method' => 'POST',
          'content' => $data
        ));
if ($optional_headers !== null) {
  $params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
  echo "error";
}
$response = @stream_get_contents($fp);
if ($response === false) {
  throw new Exception("Problem reading data");
}
  return $response;
}

$data = "access_token=".$access_token;

$url = 'https://graph.facebook.com/'.$post_id.'/likes';

$result = do_post_request($url, $data, $optional_headers = null);
?>

Also in my tested working code the $post_id has the user id whose post it was first like this: SOURCEUSERID_POSTID

Got this solution from a combination of this post - http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/

and this one - Like posts over Facebook graph api

hope this helps someone else out !

这篇关于像Facebook的外部使用Graph Api - 的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆