views:

647

answers:

2

I'm having a little trouble updating backgrounds via Twitter's API.

$target_url = "http://www.google.com/logos/11th_birthday.gif";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);

$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $html), 'POST');

When I try to pull the raw data via cURL or file_get_contents, I get this...

Expectation Failed The expectation given in the Expect request-header field could not be met by this server. The client sent Expect: 100-continue but we only allow the 100-continue expectation.

+1  A: 

Well, given the error message, it sounds like you should load the URL's contents yourself, and post the data directly. Have you tried that?

Jon Skeet
+1  A: 

OK, you can't direct Twitter to a URL, it won't accept that. Looking around a bit I've found that the best way is to download the image to the local server and then pass that over to Twitter almost like a form upload.

Try the following code, and let me know what you get.

// The URL from an external (or internal) server we want to grab
$url = 'http://www.google.com/logos/11th_birthday.gif';

// We need to grab the file name of this, unless you want to create your own
$filename = basename($url);

// This is where we'll be saving our new file to. Replace LOCALPATH with the path you would like to save the file to, i.e. www/home/content/my_directory/
$newfilename = 'LOCALPATH' . $filename;

// Copy it over, PHP will handle the overheads.
copy($url, $newfilename);

// Now it's OAuth time... fingers crossed!
$content = $to->OAuthRequest('http://twitter.com/account/update_profile_background_image.xml', array('profile_background_image_url' => $newfilename), 'POST');

// Echo something so you know it went through
print "done";
jakeisonline
Your original code is actually correct, expect where it states `'profile_background_image' => 'URL_HERE'` that URL_HERE needs to be actual data. Therefore, before you even make the OAUTH request, you need to use PHP cURL to scrape the image data from the image URL and place that within the profile_background_image option.
jakeisonline
Do you have any experience with PHP cURL at all? If not, I can certainly point you in the right direction, but there will be a slight learning curve I'm afraid
jakeisonline
I'm testing this out now, will take a look at what's actually being passed over and let you know.
jakeisonline
Alright, try it with the updated answer. Essentially you need to get the file onto your server first, whether it's via a form upload or a straight copy, then POST that to Twitter.
jakeisonline
I don't get that error anymore, I'm getting an error from Twitter instead. I think it's posting the wrong values now. I still don't get how posting $filename will help? Confused...
Homework
You have to upload to your server because it is impossible to post a file from another server, as you don't have the raw file to push through. I'll take a proper look at this later and figure it out, I only tested it with the conventional REST API and not oAuth, which worked fine.
jakeisonline
Also, when you run the script, check in the path you're copying the file to, is it actually there? Try and avoid using temporary folders, if you need to remove it after the upload to twitter, use `unlink($newfilename);`
jakeisonline
Sorry Joey, I can't solve this. I keep getting 'Incorrect signature' which just isn't right, and I'm not sure if it's the oAuth library that was written for twitter or twitter themselves. Seems a few people have this issue. I can't help.
jakeisonline
Thanks so much. I'll keep trying.
Homework
The code doesn't work
Unreality
@Unreality: Really helpful, "doesn't work" sure rules out a lot. Nonetheless, this worked fine in my own environment, using the given Twitter oAuth PHP Library.
jakeisonline
jakeisonline, I'm sorry, but which oAuth PHP Library are you using? if you are using this one http://github.com/abraham/twitteroauth , which version are you using?
Unreality
And... from http://apiwiki.twitter.com/Twitter-REST-API-Method:-account%C2%A0update_profile_background_image the required parameter is "image", but your code seems to use "profile_background_image_url"... I'm wondering how it can work
Unreality
so.. any tricks to make that work?
Unreality
Hey hey maybe you are on holiday, but I really wish you can explain the whole magic trick
Unreality
A solution that really work can be found here: http://stackoverflow.com/questions/1967831/twitter-api-updating-profile-bg-image-with-php It used the correct parameter "image" instead of "profile_background_image_url"
Unreality
Hey, great stuff, glad you found your answer. My code above worked at the time, but it's pretty old I guess. More than likely something changed.
jakeisonline