无法将照片上传到Facebook

时间:2011-12-22 20:20:31

标签: php facebook-graph-api upload photo

我正在尝试创建一个PHP页面,用于创建相册并上传照片。到目前为止,它验证和创建相册没有任何问题,但我似乎无法上传照片。我一直收到以下错误:

  

警告:fopen(https://graph.facebook.com/yyyyyyyyyyyyyyyy/photos?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)[function.fopen]:无法打开流:HTTP请求失败!第96行/home/content/30/8734730/html/uploadPhoto.php中的HTTP / 1.0 400错误请求

<html>
<head>
<title>Photo Upload</title>
</head>
<body>
<?php

$app_id = "xxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$post_login_url = "http://xyz.com/uploadPhoto.php";

$album_name = "Brand Image Productions";
$album_description = "Blah Blah event Photos";

$photo_source = 'C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg';
$photo_message = 'Here is the lighthouse';

$code = $_REQUEST["code"];
echo "code ==>" . $code . '<br/><br/>';

//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_stream,user_photos";
    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'];
    echo "access_token ==>" . $access_token . '<br/><br/>';

    // Create a new album

    $graph_url = "https://graph.facebook.com/me/albums?"
        . "access_token=". $access_token;

    $postdata = http_build_query(
        array(
            'name' => $album_name,
            'message' => $album_description
        )
    );

    $opts = array('http' =>
        array(
            'method'=> 'POST',
            'header'=> 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);
    $result = json_decode(file_get_contents($graph_url, false, $context));

    // Get the new album ID
    $album_id = $result->id;    //upload photo
    echo "album_id ==>" . $album_id . '<br/><br/>';


    // Upload the photo

    $graph_url = "https://graph.facebook.com/" . $album_id . "/photos?access_token=" . $access_token;

    echo "graph_url ==>" . $graph_url . '<br/><br/>';
    echo "photo_message ==>" . $photo_message . '<br/>';
    echo "photo_source  ==>" . $photo_source . '<br/><br/>';

    $postdata = http_build_query(
        array(
            'message' => $photo_message,
            'source' => $photo_source
        )
    );

    $opts = array('http' =>
        array(
            'method'=> 'POST',
            'header'=> 'Content-type: application/x-www-form-urlencoded',
            'enctype' => 'multipart/form-data',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);
    $fp = fopen($graph_url, 'r', false, $context);
    fpassthru($fp);
    fclose($fp);

}
?>
</body>
</html>

0 个答案:

没有答案