FACEBOOK自动发布到facebook网站(Curl)

时间:2014-01-31 12:24:44

标签: php facebook curl

我正在尝试通过cron实现自动发布到我创建的facebook网站。关于这一点的难点在于:在此过程中没有帐户登录。

我检查了很多网站以查找我的问题,不知何故它仍然无效......有没有人在我的代码中看到任何问题QQ帮助

$username = "xx";
$password = "xx";

// access to facebook home page (to get the cookies)
$curl = curl_init ();
curl_setopt ( $curl, CURLOPT_URL, "http://www.facebook.com" );
curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $curl, CURLOPT_ENCODING, "" );
curl_setopt ( $curl, CURLOPT_COOKIEJAR, getcwd () . '/cookies_facebook.cookie' );
curl_setopt ( $curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"] );
$curlData = curl_exec ( $curl );


// do get some parameters for login to facebook
$charsetTest = substr ( $curlData, strpos ( $curlData, "name=\"charset_test\"" ) );
$charsetTest = substr ( $charsetTest, strpos ( $charsetTest, "value=" ) + 7 );
$charsetTest = substr ( $charsetTest, 0, strpos ( $charsetTest, "\"" ) );

$locale = substr ( $curlData, strpos ( $curlData, "name=\"locale\"" ) );
$locale = substr ( $locale, strpos ( $locale, "value=" ) + 7 );
$locale = substr ( $locale, 0, strpos ( $locale, "\"" ) );

$lsd = substr ( $curlData, strpos ( $curlData, "name=\"locale\"" ) );
$lsd = substr ( $lsd, strpos ( $lsd, "value=" ) + 7 );
$lsd = substr ( $lsd, 0, strpos ( $lsd, "\"" ) );

// do login to facebook
curl_setopt ( $curl, CURLOPT_URL, "https://login.facebook.com/login.php?login_attempt=1" );
curl_setopt ( $curl, CURLOPT_POST, 1 );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, 
    "charset_test=" . $charsetTest . 
    "&locale=" . $locale . 
    "&non_com_login=&email=" . $username . 
    "&pass=" . $password . 
    "&charset_test=" . $charsetTest . 
    "&lsd=" . $lsd );
curl_setopt ( $curl, CURLOPT_COOKIEFILE, getcwd () . '/cookies_facebook.cookie' );
$curlData = curl_exec ( $curl );//this actually is the code of my facebook profile page so it works


//Page access_token
$access_token = "xx";   
$params=array(
      'access_token'=>$access_token, 
      'message'=> "Testing newwjjj7wwwg", 
      'name'=>'TEST6678888');

//Postng to facebook page
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/".variable_get('webconnect_facebook_pageId')."/feed");
$result = curl_exec($curl);

curl_close ($curl);

当我尝试发帖时:{ "error": { "message": "Error validating access token: This may be because the user logged out or may be due to a system error.", "type": "OAuthException", "code": 190, "error_subcode": 467 } }

当我尝试使用用户access_token:{ "error": { "message": "(#200) The user hasn't authorized the application to perform this action", "type": "OAuthException", "code": 200 } }

事情是我确实授权了......任何想法?

2 个答案:

答案 0 :(得分:0)

当用户登录Facebook并且访问令牌有效时,您的代码是否可以在浏览器会话中发布?您可以创建新的测试用户,以确保访问令牌有效并且授予了正确的权限。如果应用程序在该方案中有效,请让用户登录并尝试使用原始测试用例(使用第一次测试中的访问令牌)。这应该告诉您原始代码是否存在问题,或者是否是会话/数据(访问代码,权限)问题。

答案 1 :(得分:0)

  

当我尝试发帖时:{“error”:{“message”:“验证错误   访问令牌:这可能是因为用户已注销或可能是由于   系统错误。“,”type“:”OAuthException“,”code“:190,   “error_subcode”:467}}

这是因为令牌的生命周期已过期。您需要延长令牌的生命周期。请参阅here

  

当我尝试使用用户access_token时:{“error”:{“message”:   “(#200)用户尚未授权应用程序执行此操作   action“,”type“:”OAuthException“,”code“:200}}

为此,您需要检查权限status_update附近的复选框。见here.