facebook - file_get_contents 不适用于 facebook graph api

标签 facebook facebook-graph-api file-get-contents

file_get_contents 无法使用以下网址(无法打开流:HTTP 请求失败!HTTP/1.0 400 错误请求)

$token_url = "https://graph.facebook.com/oauth/access_token?client_id=235326466577139&redirect_uri=http%3A%2F%2Fapps.facebook.com%2Flikeablephotos%2F&client_secret=CLIENT_SECRET&code=AQDFZbjpAUda8c_gz4wDDuBOVrsn0dAp z3s8UA--7hFQIi1wb70 -tDE56xXcCtDq-hV5UWzR5YEw_ozuGT24FLfvh9KnqHZ3xdn46P_KxYCf3DHJQA3AAu2ICHBqTk1-6fHTsl6FbagKz83H6dn15kkbKksajA4KcVIoPse5JbuBLlh6V5L1ANe8fzR94iH_SMU"

$response = file_get_contents($token_url);

但是如果您将上面的 URL 复制并粘贴到浏览器地址栏中,它就可以正常工作了!并返回:

access_token=AAADWBzZAyUvMBAL2Th6CRtxh2Up5soTCK8N4HJcy0ZBhQgJPxtZArKbuITISMoGLDxNiyeNW4GUZCBvJPeBTH6mx4v83ueUIAAYQJA1WrAZDZD&expires=5112501

但他的类似 URL(对于不同的用户)也有效:

$token_url = "https://graph.facebook.com/oauth/access_token?client_id=235326466577139&redirect_uri=http%3A%2F%2Fapps.facebook.com%2Flikeablephotos%2F&client_secret=CLIENT_SECRET&code=AQC2kTEV96-1Cki2oYUhyzjH6yFe6AJRd1Q3G8fbUXW-IsLJUlactzSwCvGVBK6jh1tL-t7v6dOWJZzbkSYhk0n2z6BHQcpljWAdoXFGB5zLC4FgW8fmxT6hwdRIQOr2dZ95CD_q5yJuOUz_2DItUa3_FF9m2_TmFYGEbxPoiaF47YSTUuZp6g-8ffziJcKDAdo";

使用file_get_contents

请帮忙,谢谢

最佳答案

您是否考虑过使用 cURL 作为 file_get_contents 的替代方案。我使用curl 来处理大量请求并取得了很好的结果,并且在失败时它不会暴露您的客户端 secret 。

引用http://php.net/manual/en/book.curl.php

此代码片段是我所有应用程序获取应用程序访问 token 的标准。可用于所有 api 调用。

<小时/>
$app_access_token = GetCH();
function GetCH(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
if(substr($url,0,8)=='https://'){
    // The following ensures SSL always works. A little detail:
    // SSL does two things at once:
    //  1. it encrypts communication
    //  2. it ensures the target party is who it claims to be.
    // In short, if the following code is allowed, CURL won't check if the 
    // certificate is known and valid, however, it still encrypts communication.
    curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};

关于facebook - file_get_contents 不适用于 facebook graph api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10863959/

相关文章:

Facebook 权限

facebook - 计算 URL 的社交媒体分数

javascript - JS Facebook API错误: An active access token must be used to query information about the current user

php - 当 url 有 & 时,file_get_contents 失败

php - 如何使用 file_get_contents 在 PHP 中发布数据?

ios - 无法足够快地获取 Facebook 数据

ios - Facebook 按钮在 Safari 而不是应用程序中打开

php - file_get_contents 从谷歌获取的文件与浏览器中显示的不同

ios - 如何通过 facebook 登录您的应用程序

ruby-on-rails - 考拉在帖子上获得点赞