在Linux服务器上使用curl获取facebook的访问令牌

发布于 2024-12-08 16:39:01 字数 1630 浏览 0 评论 0原文

我在尝试使用 cURL 从 Linux 服务器上的 Facebook 获取访问令牌时遇到问题。在我的 Windows 机器上,当我在 XAMPP 下运行 PHP 时一切正常,但在 Linux 中却不行。

我已在 Linux 服务器上安装了 cURL,现在尝试使用它,但它不返回任何信息。

我的代码如下:

$url = "https://graph.facebook.com/oauth/access_token?client_id=278313225526998&redirect_uri=thejunction.africanbank.net/baobab/baobab.php&client_secret=$app_secret&code=$code";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: graph.facebook.com'));
$access_token = curl_exec($ch);
curl_close($ch); 
echo "access is: ".$access_token;

有人可以让我知道我在这里可能做错了什么吗?它给了我错误:

An error occured while fetching the URI

我也尝试使用

 $url = 'http://www.stackoverflow.com';
     //curl脚本获取给定url的内容
  $ch=curl_init();
     // 设置目标url
  curl_setopt($ch, CURLOPT_URL,$url);
  // 像 Firefox 一样请求
  curl_setopt($ch, CURLOPT_HTTPHEADER, Array("用户代理: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); 
  curl_setopt($ch, CURLOPT_NOBODY, false);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $结果=curl_exec($ch); 
  卷曲关闭 ($ch);
  echo "结果是".$result;

它给了我一个错误

An error occured while fetching the URI. Please retry.

,并且在我的 php.ini 文件中启用了curl,请参见图片 在此处输入图像描述

可能是什么问题?请帮我。

谢谢 唐纳德

I'm having a problem trying to use cURL to get the access token from Facebook on my Linux server. On my Windows machine everything works fine when I'm running PHP under XAMPP, but not in Linux.

I have installed cURL on my Linux server and am now trying to use it, but it doesn't return any information.

My code is as follows:

$url = "https://graph.facebook.com/oauth/access_token?client_id=278313225526998&redirect_uri=thejunction.africanbank.net/baobab/baobab.php&client_secret=$app_secret&code=$code";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: graph.facebook.com'));
$access_token = curl_exec($ch);
curl_close($ch); 
echo "access is: ".$access_token;

Can someone please let me know what I might be doing wrong here? It gives me the error:

An error occured while fetching the URI

I also tried using

  $url = 'http://www.stackoverflow.com';
     //curl script to get content of given url
  $ch = curl_init();
     // set the target url
  curl_setopt($ch, CURLOPT_URL,$url);
  // request as if Firefox
  curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U;   Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); 
  curl_setopt($ch, CURLOPT_NOBODY, false);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result= curl_exec ($ch); 
  curl_close ($ch);
  echo "result is".$result;

and it gives me an error

An error occured while fetching the URI. Please retry.

and in my php.ini file curl is enabled, please see the picture
enter image description here

What might be the problem guys? please help me.

Thanks
Donald

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-12-15 16:39:01

您的redirect_uri 错误。它应该

  1. 进行编码
  2. 使用 http(s)

,因此更改

$url = "https://graph.facebook.com/oauth/access_token?client_id=278313225526998&redirect_uri=thejunction.africanbank.net/baobab/baobab.php&client_secret=$app_secret&code=$code";

$url = "https://graph.facebook.com/oauth/access_token?client_id=278313225526998&redirect_uri=".urlencode("http://thejunction.africanbank.net/baobab/baobab.php")."&client_secret=$app_secret&code=$code";

Your redirect_uri is wrong. It should be

  1. encoded
  2. with http(s)

so change

$url = "https://graph.facebook.com/oauth/access_token?client_id=278313225526998&redirect_uri=thejunction.africanbank.net/baobab/baobab.php&client_secret=$app_secret&code=$code";

to

$url = "https://graph.facebook.com/oauth/access_token?client_id=278313225526998&redirect_uri=".urlencode("http://thejunction.africanbank.net/baobab/baobab.php")."&client_secret=$app_secret&code=$code";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文