title: Twitter API -> updating profile bg image with php
body: <p>So far I have been trying to update the twitter profile bg image thr the twitter api with php... and without success</p>
  
  <p>Many examples on the web, including this one:<br/>
  <a href="https://stackoverflow.com/questions/1483163/updating-twitter-background-via-api">Updating Twitter background via API</a> <br/>
  and this one<br/>
  <a href="https://stackoverflow.com/questions/1485136/twitter-background-upload-with-api-and-multi-form-data">Twitter background upload with API and multi form data</a> <br/>
  do not work at all, most ppl throw out answers without actually testing the code.</p>
  
  <p>I found that directly submit the image to the twitter.com thr html form, it will work:</p>
  
  <pre><code>&lt;form action="http://twitter.com/account/update_profile_background_image.xml" enctype="multipart/form-data" method="post"&gt;
      File: &lt;input type="file" name="image" /&gt;&lt;br/&gt;
      &lt;input type="submit" value="upload bg"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>(although the browser will prompt you for the twitter account username and password)</p>
  
  <p>However, if I want to go thr the same process with php, it fails</p>
  
  <pre><code>&lt;?php
  if( isset($_POST["submit"]) ) {
  
      $target_path = "";
      $target_path = $target_path . basename( $_FILES['myfile']['name']); 
  
      if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
          // "The file ".  basename( $_FILES['myfile']['name']). " has been uploaded&lt;br/&gt;";
      } else{
          // "There was an error uploading the file, please try again!&lt;br/&gt;";
      }
  
      $ch = curl_init('http://twitter.com/account/update_profile_background_image.xml');
      curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
      curl_setopt($ch, CURLOPT_USERPWD, $_POST['name'] . ':' . $_POST['pass']);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
      curl_setopt($ch, CURLOPT_TIMEOUT, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' =&gt; base64_encode(file_get_contents($target_path))));
  
      $rsp = curl_exec($ch);
      echo "&lt;pre&gt;" . str_replace("&lt;", "&amp;lt;", $rsp) . "&lt;/pre&gt;";
  
  }
  ?&gt;
  &lt;form enctype="multipart/form-data" method="post"&gt;
  &lt;input type="hidden" name="submit" value="1"/&gt;
  name:&lt;input type="text" name="name" value=""/&gt;&lt;br/&gt;
  pass:&lt;input type="password" name="pass" value=""/&gt;&lt;br/&gt;
  File: &lt;input type="file" name="myfile" /&gt;&lt;br/&gt;
  &lt;input type="submit" value="upload bg"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The strange thing of this code is that.. it successfully returns the twitter XML, <em>WITHOUT</em> having the profile background image updated. So at the end it still fails.</p>
  
  <p>Many thanks for reading this. It will be great if you can help. Please kindly test your code first before throwing out answers, many many thanks.</p>