2014-12-18 157 views
1

我是PHP和Facebook API的初學者。我想要一個圖片上傳到Facebook。我得到一個錯誤,指出:failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request無法打開流:HTTP請求失敗! HTTP/1.1 400錯誤請求5

下面是我的代碼示例:

<?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> 
+1

請重新格式化您的代碼。現在基本上不可讀。你一直都在努力。但是,您的網址是雙重編碼,這可能是問題的一半。 –

回答

0

在$ my_url刪除多餘的引號

$my_url="http://localhost/facebook/examples/example.php" 
相關問題