2011-04-28 5 views
14

페이스 북 사용자 ID와 액세스 토큰을 저장하고 있습니다. 이 정보로 선택한 사용자의 담벼락에 게시 할 수 있습니까? 다음 코드는 여기에 있습니다 : http://developers.facebook.com/docs/reference/api/post/ PHP로 실행하는 방법을 모르겠습니다.cURL을 사용하여 페이스 북 사용자의 벽에 게시 PHP

curl -F 'access_token=$accessToken' \ 
    -F 'message=Check out this funny article' \ 
    -F 'link=http://www.example.com/article.html' \ 
    https://graph.facebook.com/$facebookid/feed 
+0

PHP의 [cURL API] (http://ca2.php.net/manual/en/book.curl.php) 사용은 어떻게됩니까? – zneak

+0

이 방법이 효과가 있을지는 모르겠지만 시도해 보겠습니다. – Kumar

답변

30
$attachment = array(
'access_token' => $token, 
'message' => $msg, 
'name' => $title, 
'link' => $uri, 
'description' => $desc, 
'picture'=>$pic, 
'actions' => json_encode(array('name' => $action_name,'link' => $action_link)) 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/fbnameorid/feed'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output 
$result = curl_exec($ch); 
curl_close ($ch); 
+1

이 코드 어딘가에'if' 문이 있어야합니까? – pepe

+0

브라우저에서이 코드를 실행하면 언제나 작동합니다. 하지만 콘솔에서 실행할 때 { "오류": { "메시지": "사용자가이 작업을 수행하도록 응용 프로그램을 허가하지 않았습니다", "type": "OAuthException", "code": 200}} – themis

+0

감사합니다. 알아 낸 것을 추가하십시오. 서버에서 사진을 업로드하려면 요청 URL을 https://graph.facebook.com/fbnameorid/photos로 변경하고 'file'이라는 배열 항목을 값 '@'. [파일 경로] – ssaltman

2

Facebook SDK을 사용하십시오. 혼자서 CURL을 처리하는 것보다 훨씬 낫습니다.

+10

아마도 전체 SDK를 가리키는 대신 Facebook SDK의 특정 기능이이 코드를 대체 할 수 있습니다. – joe

3

나는 cURL 메소드를 시도했지만 $ action_name 및 $ action_link가 무엇인지를 모르겠다.

<?php 
require_once("facebooksdk/facebook.php"); 
require_once('config.php'); 

$facebook = new Facebook(array(
'appId' => $appId, 
'secret' => $appSecret, 
'cookie' => true 
)); 

$access_token = $facebook->getAccessToken(); 
echo $access_token; 
$msg = "testmsg"; 
$title = "testt"; 
$uri = "http://somesite.com"; 
$desc = "testd"; 
$pic = "http://static.adzerk.net/Advertisers/d18eea9d28f3490b8dcbfa9e38f8336e.jpg"; 
$attachment = array(
'access_token' => $access_token, 
'message' => $msg, 
'name' => $title, 
'link' => $uri, 
'description' => $desc, 
'picture'=>$pic, 
'actions' => json_encode(array('name' => $action_name,'link' => $action_link)) 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/me/feed'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output 
$result = curl_exec($ch); 
curl_close ($ch); 


?> 

액세스 토큰을 성공적으로 받았으므로이 두 매개 변수 만 필요합니다.

+0

다음과 같이 추가하십시오 :' \t $ action_name = '지금 등록'; \t $ action_link = 'https : //example.com/register/'; ' –

관련 문제