【问题标题】:Post Image to the Facebook using Graph API by Giving the source of the file通过提供文件源,使用 Graph API 将图像发布到 Facebook
【发布时间】:2014-07-08 07:19:23
【问题描述】:

我可以使用以下代码将图像上传到 facebook 帐户:

<?php
    include_once "facebook.php";
    ini_set("display_errors",0);

    //configuring application to post.
    $app_id = "YOUR_APP_ID";
    $app_secret = "YOUR_APP_SECRET";
    $post_login_url = "YOUR_REDIRECT_URL";

    $code = $_REQUEST["code"];

    //Obtain the access_token with publish_stream permission
    if(empty($code)){
        $dialog_url= "http://www.facebook.com/dialog/oauth?"
                . "client_id=" .  $app_id
                . "&redirect_uri=" . urlencode( $post_login_url)
                .  "&scope=publish_actions";
        echo("<script>top.location.href='" . $dialog_url
                . "'</script>");
    }
    else {
        $token_url="https://graph.facebook.com/oauth/access_token?"
                . "client_id=" . $app_id
                . "&redirect_uri=" . urlencode( $post_login_url)
                . "&client_secret=" . $app_secret
                . "&code=" . $code;
        $response = file_get_contents($token_url);
        $params = null;
        parse_str($response, $params);
        $access_token = $params['access_token'];

        // Show photo upload form to user and post to the Graph URL
        $graph_url= "https://graph.facebook.com/me/photos?"
                . "access_token=" .$access_token;

        echo '<html><body>';
        echo '<form enctype="multipart/form-data" action="'
                .$graph_url .' "method="POST">';
        echo 'Please choose a photo: ';
        echo '<input name="source" type="file"><br/><br/>';
        echo '<input type="submit" value="Upload"/><br/>';
        echo '</form>';
        echo '</body></html>';
    }
?>

但是正如你所看到的,我使用表单标签和提交按钮来发布使用输入类型为文件的图像。 但我不想浏览图像。 我想删除所有浏览代码,只想提供图片来源以将该图片上传到 Facebook 帐户。

如下所示:

<?php
    include_once "facebook.php";
    ini_set("display_errors",0);

    //configuring application to post.
    $app_id = "YOUR_APP_ID";
    $app_secret = "YOUR_APP_SECRET";
    $post_login_url = "YOUR_REDIRECT_URL";

    $code = $_REQUEST["code"];

    //Obtain the access_token with publish_stream permission

    if(empty($code)){
        $dialog_url= "http://www.facebook.com/dialog/oauth?"
                . "client_id=" .  $app_id
                . "&redirect_uri=" . urlencode( $post_login_url)
                .  "&scope=publish_actions";
        echo("<script>top.location.href='" . $dialog_url
                . "'</script>");
    }
    else {
        $token_url="https://graph.facebook.com/oauth/access_token?"
                . "client_id=" . $app_id
                . "&redirect_uri=" . urlencode( $post_login_url)
                . "&client_secret=" . $app_secret
                . "&code=" . $code;
        $response = file_get_contents($token_url);
        $params = null;
        //echo "Response: $response<br>";
        parse_str($response, $params);
        $access_token = $params['access_token'];
    //  echo "Access Token: $access_token<br>"; 

        // Show photo upload form to user and post to the Graph URL
        $graph_url= "https://graph.facebook.com/me/photos?access_token=" .$access_token;

        echo '<html><body>';
        echo '<form action="'.$graph_url .'"method="POST">';
        //echo '<input name="source" type="file"><br/><br/>'; Putting img tag instead of file
        echo '<img src="'.get_template_directory_uri().'/download.php?f='.apply_filters('filter_if_add_to_cart',$image_link).'"><br/><br/>';
        echo '<input type="submit" value="Upload"/><br/>';
        echo '</form>';
        echo '</body></html>';
    }
?>

但我不知道该怎么做。 你能帮我解决这个问题吗? 谢谢,

更新:我已经更新了我的代码。请参阅下面的更新代码:

<?php
    include_once "facebook.php";
    ini_set("display_errors",0);

    $img_src = $_SESSION['imgSrc'];

    //configuring application to post.
    $app_id = "YOUR_APP_ID";
    $app_secret = "YOUR_APP_SECRET";
    $post_login_url = "YOUR_REDIRECT_URL";

    $code = $_REQUEST["code"];

    //Obtain the access_token with publish_stream permission
    if(empty($code)){
        $dialog_url= "http://www.facebook.com/dialog/oauth?"
                . "client_id=" .  $app_id
                . "&redirect_uri=" . urlencode( $post_login_url)
                .  "&scope=publish_actions";
        echo("<script>top.location.href='" . $dialog_url
                . "'</script>");
    }
    else {
        $token_url="https://graph.facebook.com/oauth/access_token?"
                . "client_id=" . $app_id
                . "&redirect_uri=" . urlencode( $post_login_url)
                . "&client_secret=" . $app_secret
                . "&code=" . $code;
        $response = file_get_contents($token_url);
        $params = null;
        //echo "Response: $response<br>";
        parse_str($response, $params);
        $access_token = $params['access_token'];

        //Posting Image to The Facebook

        $api_url = 'https://graph.facebook.com/v2.0/me/photos';
        $attachment =  array(
                'url'  => "{$img_src}",
                'access_token' => $access_token
        );
        $api_response = UseCurl($api_url, $attachment);
        $post_result = json_decode($api_response, TRUE);
        print_r($post_result);

        function UseCurl($url, $attachment){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            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_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($ch);
            curl_close ($ch);

            return $result;
        }
    }
?>

【问题讨论】:

  • 也就是说你的图片在服务器上对吧?

标签: php facebook facebook-graph-api facebook-php-sdk


【解决方案1】:

如果您在服务器中有想要发布的图片,您可以简单地使用url 参数来上传照片。就这样-

$api_url = 'https://graph.facebook.com/v2.0/me/photos';
$attachment =  array(
   'url'  => "{image-url}",
   'access_token' => $access_token
);
$api_response = UseCurl($api_url, $attachment);
$post_result = json_decode($api_response, TRUE);
print_r($post_result);

function UseCurl($url, $attachment){
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   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_HEADER, 0);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $result = curl_exec($ch);
   curl_close ($ch);

   return $result;
}

【讨论】:

  • 我已经更新了我的代码...请在“更新:”下的问题中查看它是对的吗?
  • 是...“致命错误:调用 /var/www/picpixa2/wp-content/plugins/create-own-object/plugin-facebook/postToFB.php 中的未定义函数 UseCurl()在第 42 行调用堆栈:0.0006 349492 1. {main}() /var/www/picpixa2/wp-content/plugins/create-own-object/plugin-facebook/postToFB.php:0 "
  • 您在if 语句中编写函数?这是 PHP 中的一个非常基本的错误,我没想到你会出现这个错误
  • 出现这个错误:Array ( [error] => Array ( [message] => 发生未知错误。[type] => OAuthException [code] => 1 ) )
  • 可能是 OAuth 问题。尝试注销,然后使用新的访问令牌再次登录,然后尝试
猜你喜欢
  • 1970-01-01
  • 2014-08-24
  • 2022-01-19
  • 2011-06-21
  • 2013-06-18
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多