title: How to use Facebook Graph API to upload image to Apps own page?
body: <p>I'm trying to create a PHP script to upload photos to an Apps own Fan Page and for them to appear large in the Fan Page timeline. Similar to <a href="https://www.facebook.com/Instagram" rel="nofollow">https://www.facebook.com/Instagram</a>. </p>
  
  <p>This is the code I have but although it's using page_id in the OpenGraph URL it keeps posting to my personal photo albums, not the Page albums.</p>
  
  <pre><code>&lt;?php
  
      $app_id = "APP_ID";
      $app_secret = "APP_SECRET";
      $post_login_url = "LOGIN_URL";
      $fan_page_id = "PAGE_ID";
  
      $code = $_REQUEST["code"];
  
      //Obtain the access_token with publish_stream,manage_pages permission 
      if(empty($code)){ 
          $dialog_url= "http://www.facebook.com/dialog/oauth?"
          . "client_id=" .  $app_id 
          . "&amp;redirect_uri=" . urlencode( $post_login_url)
          . "&amp;scope=publish_stream,manage_pages";
          echo("&lt;script&gt;top.location.href='" . $dialog_url . "'&lt;/script&gt;");
      }
      else {
          $token_url="https://graph.facebook.com/oauth/access_token?"
          . "client_id=" . $app_id 
          . "&amp;redirect_uri=" . urlencode( $post_login_url)
          . "&amp;client_secret=" . $app_secret
          . "&amp;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/".$fan_page_id."/photos?access_token=" .$access_token;
  
      echo '&lt;html&gt;&lt;body&gt;';
      echo '&lt;form enctype="multipart/form-data" action="' .$graph_url .' "method="POST"&gt;';
      echo 'Please choose a photo: ';
      echo '&lt;input name="source" type="file"&gt;&lt;br/&gt;&lt;br/&gt;';
      echo '&lt;input type="submit" value="Upload"/&gt;&lt;br/&gt;';
      echo '&lt;/form&gt;';
      echo '&lt;/body&gt;&lt;/html&gt;';
      }
  ?&gt;
  </code></pre>
  
  <p>Code sample v2 which doesn't return anything from CURL</p>
  
  <pre><code>    $fb_page_id = "fb_page_id";
      $user_id = 'user_id'; 
      $fb_access_token = 'fb_access_token';
  
      $args = array(
          'url' =&gt; 'http://www.mysite.com/savefiles/smalls/comps/1/comp_1_600.jpg',
          'message' =&gt; 'SOME CAPTION TEXT'
      );
  //Get Page Access Token - (as opposed to your user access token - requires manage_pages permission)
      $accounts = 'https://graph.facebook.com/'.$user_id.'/accounts?access_token='.$fb_access_token;
      $accounts_data = file_get_contents($accounts,0,null,null);
      $account_data = json_decode($accounts_data, true);
  
      foreach ($account_data['data'] as $accountdata) {
  
          $account_page_id = $accountdata['id'];
          $page_access_token = $accountdata['access_token'];
          if($account_page_id == $fb_page_id){
              $my_page_access_token = $page_access_token; 
              //echo $my_page_access_token . '&lt;br/&gt;';
          }   
      }
  
      //Not Tested - Just in Case     
      if($my_page_access_token==''){
          $my_page_access_token = $fb_access_token;
      }
  
  //Make Upload Call
      $ch = curl_init();
      $url = 'https://graph.facebook.com/'.$fb_page_id.'/photos?access_token='.$my_page_access_token;
     // echo $url . '&lt;br/&gt;';
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
      $data = curl_exec($ch);
      $post_object = json_decode($data);
      $post_data = get_object_vars($post_object);
      $post_id = $post_data['post_id'];
      curl_close($ch);
  </code></pre>