2013-07-03 90 views
2

这是我的代码,我试图“卷曲”它(file_get_contents没有出于某种原因),但因为我不能真正得到卷曲工作,我来到这里得到一些帮助。Facebook的图片上传(粉丝专页)

我一直在痛苦与这10个小时,我仍然无法找到!请帮忙!!

<?php 
$app_id = "xxxxx"; 
$app_secret = "xxxxx"; 
$fanpage_id ='3xxxxx'; 

$post_login_url = "xxxxxxxxxteszt.php"; 
$photo_url = "xxxxxxxxxx20130412104817.jpg"; 
$photo_caption = "sasdasd"; 
$code = $_REQUEST["code"]; 

//Obtain the access_token with publish_stream permission 
if (!$code) 
{ 
    $dialog_url= "http://www.facebook.com/dialog/oauth?" 
    . "client_id=" . $app_id 
    . "&redirect_uri=" . urlencode($post_login_url) 
    . "&scope=publish_stream,manage_pages"; 
    echo("<script>top.location.href='".$dialog_url."'</script>"); 
} 
if(isset($_REQUEST['code'])) 
{ 
    print('<script>alert("asd");</script>'); 
    function curl($url) 
    { 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL,$url); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $url); 
    } 
    $token_url="https://graph.facebook.com/oauth/access_token?" 
    . "client_id=" . $app_id 
    . "&client_secret=" . $app_secret 
    . "&redirect_uri=" . urlencode($post_login_url) 
    . "&code=" . $code; 
    print($code); 
    $response = curl($token_url); 
    print($response); 
    $params = null; 
    parse_str($response, $params); 
    $access_token = $params['access_token']; 
    // POST to Graph API endpoint to upload photos 
    $graph_url= "https://graph.facebook.com/".$fanpage_id."/photos?" 
    . "url=" . urlencode($photo_url) 
    . "&message=" . urlencode($photo_caption) 
    . "&method=POST" 
    . "&access_token=" .$access_token; 
    echo '<html><body>'; 
    echo curl($graph_url); 
    echo '</body></html>'; 
} 
?> 
+1

请只使用PHP SDK,而不是编码这样的东西“,由手”。 https://developers.facebook.com/docs/reference/php/ – CBroe

回答

4

您不能只传递网址到CURLOPT_POSTFIELDS选项。基本URL应该在CURLOPT_URL选项进行设置,并在CURLOPT_POSTFIELDS参数(使用PHP的http_build_query函数以生成编码的查询字符串);

所以你的卷曲功能可能是这个样子:

function curl($url,$parms) 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parms, null, '&')); 
} 

然后你会这样称呼它:

$token_url="https://graph.facebook.com/oauth/access_token"; 
$token_parms = array(
    "client_id" => $app_id, 
    "client_secret" => $app_secret, 
    "redirect_uri" => $post_login_url, 
    "code" => $code 
); 
$response = curl($token_url, $token_parms); 
相关问题