我是PHP和Facebook API的初学者。我想上传一张图片到facebook。我收到一条错误消息:failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
以下是我的代码示例:
<?php
error_reporting(E_ALL & ~E_NOTICE); $app_id="xxxxxxxxxxxxx" ; $app_secret="xxxxxxxxxxxxxxxxxxxxxxxxxxx" ; $my_url="`http://localhost/facebook/examples/example.php`" ; $perms_str="publish_stream" ; $code=$ _REQUEST[ "code"]; if(empty($code)) { $auth_url="http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url). "&scope=" . $perms_str; echo( "<script>top.location.href='" . $auth_url . "'</script>"); } $token_url="https://graph.facebook.com/oauth/access_token?client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url).
"&client_secret=" . $app_secret. "&code=" . $code; $response=f ile_get_contents($token_url); $p=n ull; parse_str($response, $p); $access_token=$ p[ 'access_token']; $graph_url="https://graph.facebook.com/me/photos?" . "access_token=" .$access_token; if (!empty($_FILES))
{ $params=a rray(); if( isset($_POST[ 'message']) ) { $params[ 'message']=t rim($_POST[ 'message']); } $uploaddir='./uploads/' ; // Upload folder $uploadfile=$ uploaddir . basename($_FILES[ 'source'][ 'name']); if (move_uploaded_file($_FILES[ 'source'][
'tmp_name'], $uploadfile)) { $params[ 'source']="@" . realpath($uploadfile); } // Start the Graph API call $ch=c url_init(); curl_setopt($ch, CURLOPT_URL,$graph_url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch,
CURLOPT_POSTFIELDS, $params); $result=c url_exec($ch); $decoded=j son_decode($result, true); curl_close($ch); if(is_array($decoded) && isset($decoded[ 'id'])) { $msg="Image uploaded successfully: {$decoded['id']}" ; } }
?>
<!doctype html>
<html>
<head>
<title>Upload</title>
</head>
<body>
<?php if( isset($msg) ) { ?>
<p id="msg">
<?php echo $msg; ?>
</p>
<?php } ?>
<form enctype="multipart/form-data" action="" method="post">
<p>
<label for="name">Caption</label>
<input type="text" name="message" value="" />
</p>
<p>
<label for="source">Image</label>
<input type="file" name="source" />
</p>
<p>
<input type="submit" value="Upload" />
</p>
</form>
</body>
</html>
答案 0 :(得分:0)
删除$ my_url
中的额外引号$my_url="http://localhost/facebook/examples/example.php"