[UPDATE Jan 19th, 2008]
I’ve just modified RESTProxy.php to handle gzip encoding from the client-side. It was quite simple. I was trying to consume web services on Discogs.com and it requires that you accept gzip encoding.. The source has been modified.
Mise en situtation
for those who have been following the development of the AS3 Youtube Data API library development, you might have seen that I hit a serious wall when I tried doing PUT and DELETE requests using flash.net.URLRequest together with flash.net.URLLoader. Simply put, it is impossible to execute such requests directly from the code library provided in Flash CS3. Some people have been trying to get around the problem using raw sockets, but will hit another wall inevitably: you are only allowed to connect raw sockets to the host that served you the .swf file. Hence, impossible to connect to gdata.youtube.com.
I had another idea, using Flex’s HTTPService inside of code compiled using Flash CS3. Hit another wall there. You can dynamically load some of Flex’s classes, like StringUtil, for example, using a Loader + the applicationDomain to get a reference to the Classes embedded in a SWF then instantiating them; but it doesn’t work on my complex Flex classes. You get weird errors, which I don’t understand. After spending a day on that, I decided it was over.
My goal was to keep this library client-side, AS3 only. I didn’t mind loading a SWF to get some of it’s classes, but I didn’t want to use a proxy on a server… and well, it can’t be done.
In comes the proxy
The idea is to provide a very general, generic proxy to help AS3 become RESTful. I had a few ideas, and in the end decided to opt for AMFPHP because of my past experience with it, and the fact that since Patrick Mineault (who has been a friend here in Montreal) has left, it seems it doesn’t get much attention. AMFPHP now talks AMF3, which I think is pretty cool.
Prerequisits & Installation
In order to use this proxy you will need:
Then all you need is to copy the RESTProxy.php file to your amfphp/services folder and VOILÀ! You are ready to run!
How it works
Put simply, you call a function on your RESTProxy service, which will do the call for you to the service you are trying to be RESTful with. There is complete flexibility here, you can do everything you want. The service then returns all the information required to parse the answer from the service. This makes it so much easier, since it handles all the HTTP Response codes w/o making a fuss (like URLLoader would when it receives a 201 Created code for example. Everything is transparent.
Here is the service’s signature
The getVars is an array of string in the same format you would see them in a URL : countedHits=329. Same for the postVars.
The headers is an array of string each in the same format as the actual HTTP headers: Keep-alive: false.
and the returned values
Here is some source code that describes how to use it. This particular code will log you into the Google Data webservice so that you can use the YouTube services. (Please replace USERNAME & PASSWORD to by your YouTube credentials for it to work).
You can download the source file here.
You can use mine for testing purposes: http://www.martinlegris.com/amf3/gateway.php . However PLEASE install it on your server once you start being serious about it. If there is abuse I will remove it.
I did NOT test it on PUT requests with content yet, and I am very curious to see if it’d be possible to do file uploads using this service. I think so. If anybody tries it before me, please let me know!
cool stuff! using amfphp as a proxy is really a good idea.
any progress at uploading videos? I can’t send multipart/related forms without getting an IO error… (error 400 google)
Hi, I was looking for a getter in your VideoData.as class to find the .flv url to directly load a video into my app but I didn´t find it.
I´ve seen some samples to do this (http://thanksmister.com/examples/flexyoutube/srcview/index.html)
and it would be good to be inside your api.
xleon: I will add it to the next update. For now you have the .swfUrl easily accessible on all VideoData types. I didn’t put the flvUrl because you needed to hack it (get a special id code from the webpage containing the video on youtube) and append it to the flvUrl in order to play it. Otherwise it will not work. Maybe this has changed. I will look into it and post a solution next week.
Great! that would be nice, and I hope it has changed. because I don´t see other way to make a youTube app without youTube logo and info without that.
Although you can “hack” the way to get the FLV url I don’t think it is acceptable. I haven’t read all the Terms and Copyright policies from Youtube but one way that they make money out of the site is by allowing you to play the videos within their interface. Trying to get the FLV without using their interface it would be stealing bandwidth, as simple as that.
Now, if you don’t like Youtube’s player/interface but still want to have your videos online you can upload your videos to your server and use those instead with your own player/interface.
I tried to get the proxy script to work on various servers set up with php but I never get anything returned?! is there something I’m missing from this setup?? I’d love to get this proxy implemented cuz then i could modify it for diff services…
any help would be appreciated..
p.s. i know the problem isn’t that clear but I’ve tried so many options i’m basically..out of options 🙂
First of all thanks to martin for doing this great work really good.
Secondly Can anyone please tell me whether you need this rest webservice to retrieve information from youtubeAPI or is this a seperate thing?
the answer is: if you just want to get data from the youtubeAPI, you don’t need to use RESTProxy, however if you want to commit data (log a user in, modify his favorites, add videos), then you do need RESTProxy.
Thanks Martin. I used your script to read feeds from Friendfeed since they don’t host a cross-domain file. Check it out at http://alekkus.com/blog/2009/02/hooking-up-to-friendfeed-api/
One issue came up though; unicode/non-latin characters got messed up. I overcome it by inserting $result = rawurlencode( $result ). Then on the Flex side, I used decodeURIComponent( result ) to decode it. Works well so far.
Once again, thanks for sharing.
I’d like to use your YouTube data API in a Flash project but I’m hoping to use the new features of the API such as adding comments. Do you have an ETA for when these will be implemented?
Why don’t you just pull the HTTPService class out of flex. The framework is open source so it shouldn’t be too big of a deal. The proxy solution, while clever, presents major issues if you’re writing anything for scale.
I tried extracting HTTPService from Flex, lost 1.5 days and got nowhere. Flex is very much spaghetti code (yes, somebody has to say it!). If anybody succeeds at doing this, please show me how!
Has somebody actually tried to do the upload part with the RESTProxy?
I’m very interested on it, but all my tries were unsuccessful. I’ve seen in the AS3 Youtube Data API that xml content is sent to RESTProxy as postvars, so it’s not the same to get an upload token or to upload a file?
Please somebody help me
I think, this is a Flash player problem rather then Flash CS3. You will have same issues if you use Flex.
Here is a link to an example with Flex client using Restful service using JS / ExternalInterface.
I don’t see any problems implementing the same in Flash though personally I have not done this yet…
And here is the link which explains how to use Flex classes within Flash:
I implemented this successfully for WebService class within Flex.
Oops, forgot to post the link for Flex / Restful example with JS and ExternalInterface, here it is:
[…] om net als bij Unite de … Paul Tondeur // Jan 31, 2009 at 9:52 pm. Er is gefilmt gedurende de …intense creativity Getting around AS3’s un-RESTful-ness[UPDATE Jan 19th, 2008] I’ve just modified RESTProxy.php to handle gzip encoding from the … […]